Jaiff
Jaiff

Reputation: 487

Javascript validation not working?

What's wrong in it why it's not working...

 <script language="JavaScript" type="text/javascript">


//function to check empty fields

function isEmpty(strfield1, strfield2) {
  //change "field1, field2 and field3" to your field names

  strfield1 = document.forms[0].name.value 
  strfield2 = document.forms[0].email.value

  //name field

  if (strfield1 == "" || strfield1 == null || !isNaN(strfield1) || strfield1.charAt(0) == ' ') {
    alert( "Name is a mandatory field.\nPlease amend and retry.")
    return false;
  }

  //EMAIL field 
  if (strfield2 == "" || strfield2 == null || !isNaN(strfield2) || strfield2.charAt(0) == ' ') {
    alert(" Email is a mandatory field.\nPlease amend and retry.")
    return false;
  }
  return true;
}


//function to check valid email address

function isValidEmail(strEmail){
  validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
  strEmail = document.forms[0].email.value;

 // search email text for regular exp matches

  if (strEmail.search(validRegExp) == -1)  {
    alert('A valid e-mail address is required.\nPlease amend and retry');
    return false;
  } 
  return true; 
}


//function that performs all functions, defined in the onsubmit event handler

function check(form)){
  if (isEmpty(form.field1)){
    if (isEmpty(form.field2)){
      if (isValidEmail(form.email)){
        return true;
      }
    }
  }
}
return false;
}

</script>

It doesn't do anything I don't understand what's going there and in form I put this too

<form onsubmit="return check(this);" action="sendquery.php" name="contquery">

Upvotes: 0

Views: 591

Answers (2)

mplungjan
mplungjan

Reputation: 177685

First glance: too many brackets as shown by @FishBasketGordo so I will not repeat

Second glance - you pass the field and do not test the field value

Third glance: You do not pass the correct names to the function

Fourth glance - isEmpty returns false when empty. It should return true

I fixed all those

DEMO HERE

Complete page to show where what goes. Updated to do unobtrusive event handling on the form

<html>
<head>
<title>Validation</title>
<script type="text/javascript">

// trim for IE
if(typeof String.prototype.trim !== 'function') {
  String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, '');
  }
}

//function to check empty fields

function isEmpty(objfld) {
  var val = objfld.value;
  if (val.trim() == "" || val == null) {
    alert(objfld.name+" is a mandatory field.\nPlease amend and retry.");
    objfld.focus();
    return true;
  }
  return false;
}

//function to check valid email address

function isValidEmail(objEmail){
  var validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
  var strEmail = objEmail.value;
  if (strEmail.match(validRegExp)) return true;
  alert('A valid e-mail address is required.\nPlease amend and retry');
  objEmail.focus();  
  return false;
}

//function that performs all functions, defined in the onsubmit event handler

function validate(form) {
  if (isEmpty(form.name)) return false;
  if (isEmpty(form.email)) return false;
  return isValidEmail(form.email);
}


window.onload=function() {
  document.getElementById("form1").onsubmit=function() {
    return validate(this);
  }
}

</head>
<body>
<form id="form1">
  Name:<input type="text" name="name" /><br/>
  Email:<input type="text" name="email" /><br/>
  <input type="submit" />
</form>    
</body>
</html>

Upvotes: 3

FishBasketGordo
FishBasketGordo

Reputation: 23122

Probably the main reason it isn't working is the syntax errors:

// Syntax error ----v
function check(form)){
    if (isEmpty(form.field1)){
        if (isEmpty(form.field2)){
            if (isValidEmail(form.email)){
                return true;
            }
        }
    }
}
// The return statement should be above the previous closing bracket 
// and the final closing bracket removed.
return false;
}

There's an extra closing paren on the first line, and there are too many closing brackets. If you open up this up in FireBug or Chrome Developer Tools or a similar tool, it would tell you about this automatically.

Upvotes: 2

Related Questions