开发者

My Form Validation is not working properly

I am doing the email field validation. For this I have written a html code like this:

<form action="#">
           <span class="email-field" id="email-field">
             <input type="text" name="email-id" onFocus="if(this.value=='Your email ID'){this.value=''}" onBlur="if(this.value==''){this.value='Your email ID'}" id="email-id" />
             <input type="text" name="email-id" onFocus="if(this.value=='Please enter email ID'){this.value=''}" onBlur="if(this.value==''){this.value='Please enter email ID'}" id="error-email-id" class="hide"/>
           </span>
           <input type="submit" id="submit-button" class="subscribe-button" value="">

</form>

The Jquery I have writte is for this:

$(document).ready(function() {
  var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
  URL = 'http://localhost:8000/'


  $("#submit-button").click(function() {
   email = $("#email-id").val()

 if($.trim($("#email-id").val()) === 'Your email ID' || !(filter.test($("#email").val())) ) {
  $('#error-email-id').fadeIn('slow').removeClass("hide");
  $('#email-id').fadeOut('normal').addClass("hide");
  $('#email-field').addClass("error-highlight");
  return false;
 }
 else {

  $.ajax({
  type: 'POST',
  url: URL + "validate/" ,
  data: {"email-id": email},
  success: function(data) {
  },
  dataType: "json",
  });
  $('#form').fadeOut('normal').addClass("hide");
  $('#notification').fadeIn('slow').removeClass("show")
  return false;
 }


  });

The scnario is like this:- 1. First time I enter the email and if it correct , it makes a successful call to that url. 2. If I enter the wrong email or if validation fails it gives me the appropriate class that is the text input field of id 'error-email-id'. But When I click submit button No JS call h开发者_C百科appen. That is It doesn't call Any Jquery So no URL. It remains a static one.

Thanks in Advance !!!


By using return false; you prevent the form from being submitted in the normal way. You need to manually submit the form if validation succeeded (assuming that you add the ID regForm to the form element):

$.ajax({
  type: 'POST',
  url: URL + "validate/" ,
  data: {"email-id": email},
  success: function(data) {
    $('#regForm').submit();
  },    // ...

You probably want to check the server's response data (from the server-side part of form validation) within the success function before you submit the form.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜