Jump to content

What's Wrong With My Form Validator?


hybrid kill3r

Recommended Posts

This is my code. I could leave the form completely blank, and it will still submit.

  <script LANGUAGE="JavaScript" TYPE="text/javascript">function check() {var username = document.register.username;var password = document.register.password;var passcnf = document.register.passcnf;var email = document.register.email;var emailcnf = document.register.emailcnf;if ( username.value.indexOf(" ") == -1) {alert("You did not enter a username.");username.focus();} else if (! (parseInt(username.value) > 20)) {alert("The length of your username must not exceed 20 characters.");username.focus();} else if (! (parseInt(username.value) < 6 ) {alert("The length of your username must not be less than 6 characters.");username.focus();} else if (! (parseInt(password.value) > 20){alert("The length of your password must not exceed 20 characters.");password.focus();} else if(! (parseInt(password.value) < 6){alert("The length of your password must not be less than 6 characters.");password.focus();} else if(! (password.value != passcnf.value)){alert("The passwords you entered did not match.");} else if ((email.value.indexOf("@") == -1) || (email.value.indexOf(".") == -1)) {alert("The email address you entered was not valid.");email.focus();} else if (email.value != emailcnf.value){alert("The email addresses you entered did not match.");} else if {document.register.submit();}}</SCRIPT>

Link to comment
Share on other sites

Do you have that function plugged into your form in any way, e.g. via a FORM onsubmit (in the HTML or the JS)?If you use the onsubmit event-handler, you will need to return true if the submission is valid or return false otherwise.

Link to comment
Share on other sites

Even so, it won't work. For one, parseInt does not return a length. You want string.length. Like this:str = "hello";// str.length returns 5// note: length is a property, not a method, so don't go crazy and add ()It's past time to stop using dot construction to get references to form elements.document.register.usernameGive your element a ID and reference it like this:document.getElementById("my_id");And while we're at it, stop declaring a language attribute in your javascript tag. Type is the standard sttribute. Fortunately, you already have it, so just delete the other.

Link to comment
Share on other sites

Do you have that function plugged into your form in any way, e.g. via a FORM onsubmit (in the HTML or the JS)?If you use the onsubmit event-handler, you will need to return true if the submission is valid or return false otherwise.
Well, this is what my submit button looks like:
<input type='submit' value='Register' name='submit' onClick="check();return false;">

Would that be correct?

Link to comment
Share on other sites

No, its better to attach the function to the form's onsubmit event (if the user presses Enter on one of the input fields that click event won't fire) and returning false all the time will mean the user will never be able to submit.

<form action="page" method="post" onclick="return check()">

Then your check() function may have bits looking like this

if ( username.value.indexOf(" ") == -1) {alert("You did not enter a username.");username.focus();return false;}//.....else return true; //(remove the submit() call)

Link to comment
Share on other sites

Er, Synook, considering your lesson, did you mean this:

<form action="page" method="post" onclick="return check()">

or this:

<form action="page" method="post" onsubmit="return check()">								  ^^^^^^^^

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...