Jump to content

Having trouble with simple form validation


Recommended Posts

edit: Nevermind, I figured it out. I didn't realize I needed to write "return" inside of the onsubmit eventHi, I'm pretty new to JavaScript and am trying to make the most basic form validation, yet it is not working. The problem is, it catches the error, but the form still submits after the user clicks ok on the alert box.Here's the code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><title>Untitled Document</title><link href="formStyle.css" type="text/css" rel="stylesheet"><script type="text/javascript" language="javascript">function checkForm (passForm) {	if (passForm.user.value == "") {		alert("You muster enter a user name");		passForm.user.focus();		return false;	}}</script></head><body><form onSubmit="checkForm(this)" name="login" action="test.php">	Username: <input type="text" name="user" id="userName" />	<input type="submit" value="Login"></form></body></html>


Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...