Take a look at this webpage. It's a form that is not validating properly. It works really well until the form asks you for your email address. Then it seems to get stuck. It should go on and validate 2 more fields then go to action, which is set to email the results to me.
Wow, that code was a nightmare!
Here, I tried to make it prettier (and I think it works now, too )
Code:
<script language="javascript">
function echeck(str) {
var at="@";
var dot=".";
var lat=str.indexOf(at);
var lstr=str.length;
var ldot=str.indexOf(dot);
if (str.indexOf(at)==-1) {
alert("Invalid E-mail ID");
return false;
}
if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) {
alert("Invalid E-mail ID");
return false;
}
if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) {
alert("Invalid E-mail ID");
return false;
}
if (str.indexOf(at,(lat+1))!=-1) {
alert("Invalid E-mail ID");
return false;
}
if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) {
alert("Invalid E-mail ID");
return false;
}
if (str.indexOf(dot,(lat+2))==-1) {
alert("Invalid E-mail ID");
return false;
}
if (str.indexOf(" ")!=-1) {
alert("Invalid E-mail ID");
return false;
}
alert ("Email check completed");
return true;
}
function ValidateEmail() {
var emailID=document.joiningForm.txtEmail;
if ((emailID.value==null) || (emailID.value=="")){
alert("Please Enter your Email ID");
emailID.focus();
return false;
}
if (echeck(emailID.value)==false){
emailID.value="";
emailID.focus();
return false;
}
alert ("Yay, makes it to this point");
return CheckrestofForm();
}
function validateForm() {
//check fullname
if (document.forms[0].elements[0].value=="") {
alert ("Please enter your name");
return false;
}
if (document.forms[0].elements[1].value=="") {
alert("Please enter a username");
return false;
}
//check password
if (document.forms[0].elements[2].value.length<6) {
alert ("Please enter a password of at least 6 characters");
return false;
}
if (document.forms[0].elements[2].value != document.forms[0].elements[3].value) {
alert ("Please make sure passwords match");
return false;
}
//check email 4
if (document.forms[0].elements[4].value=="") {
alert ("Please enter your email address");
return false;
}
return ValidateEmail();
}
function CheckrestofForm() {
//check age group 5
//check identity 6
//check location 7
if (document.forms[0].elements[7].value=="") {
alert ("Please enter your location");
return false
}
//check chatroom 8
//check biography 13
if (document.forms[0].elements[13].value.length<50) {
alert ("Please tell us a little bit more about yourself");
return false;
}
return true;
}
</script>
The big problem was that your main validation function was ALWAYS returning false, no matter what (if the email was blank, it returned false, otherwise, it performed a check then, ignoring the results, returned false anyway)
Last edited by HaganeNoKokoro; 09-13-2004 at 03:16 AM.
Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.
Bookmarks