0

The span's innerHTML change depends on AJAX. If the span's innerHTML is <img src="images/not-available.png">, then submit should return false.

HTML:

<form  onsubmit="return validateemail()">
<span id= "message"><img src="images/not-available.png"></span>
<button id='submit' name="submit" type='submit'>Submit</button>
</form>

JS:

function validateemail()
{
      var munx = document.getElementById("message").innerHTML;

      var muny = '<img src="images/not-available.png">';
      if (munx == muny)
      {
      alert ("Email id is already Exist");
      return false;
      }
}

I tried this code, but it doesn't work.

user3419304
  • 249
  • 2
  • 4
  • 20
  • 5
    you should return false if for a validation issue and return true if validation is correct. The form will get submitted if you return true. – AurA Apr 16 '14 at 05:22

2 Answers2

0

You can use jQuery Validation Plugin (http://jqueryvalidation.org/validate/) in your project. This is standard form validation that you can use in diffrent way.

for example:

$(".selector").validate({
  invalidHandler: function(event, validator) {
    // 'this' refers to the form
    var errors = validator.numberOfInvalids();
    if (errors) {
      var message = errors == 1
        ? 'You missed 1 field. It has been highlighted'
        : 'You missed ' + errors + ' fields. They have been highlighted';
      $("div.error span").html(message);
      $("div.error").show();
    } else {
      $("div.error").hide();
    }
  }
});

example of ajax: Submits the form via Ajax when valid.

$(".selector").validate({
  submitHandler: function(form) {
    $(form).ajaxSubmit();
  }
});
Mohsen Rasouli
  • 352
  • 3
  • 9
  • 22
-2

You are validating it wrong.

see this:

 <span id= "message"><img src="images/not-available.png"></span>

and now see this:

 var muny = '<img src="images/not-available.png"/>';

can you notice the extra "/" character in second one i.e just before img closing bracket. Thats causing the problem.

So replace with this one:

var muny = '<img src="images/not-available.png">';
Sankalp Bhatt
  • 1,154
  • 13
  • 17