2

I'm trying to validate user's input before they submit the form. Although I'm successful doing so when they hit the button, somehow I couldn't make it also stop when they press the enter key:

HTML:

   <form method='post' class='submit_form' action="<?php echo base_url();?>interview/reserve_temporary">
        <input type="text" class="form-control email_input" id="reserve_email_2" placeholder="Email">
        <button type="button" id="subscribe_2" class="btn btn-default text-center foot-submit-btn subscribe_button">Let me try</button>
   </form>

JS:

  $('.submit_form').submit(function(e) { 

      var email = $(this).find('.email_input').val();
      var id=$(this).find('.subscribe_button').attr('id').split("_").pop(); 

      var regex_email = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

      if (regex_email.test(email)||email.match(/^\d+$/)) {
          $(this).text('Sending……');
      }
      else {
          return false;
      }
 } 
2
  • What you want to validate? Check if user press the submit button with empty string? Commented Jun 10, 2014 at 0:43
  • Check if the string they put in meet the regex requirement. Commented Jun 10, 2014 at 0:57

2 Answers 2

2

Give this a shot:

$('.submit_form').submit(function(e) { 

    var email = $(this).find('.email_input').val();
    var id=$(this).find('.subscribe_button').attr('id').split("_").pop(); 

    var regex_email = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

    if (regex_email.test(email)||email.match(/^\d+$/)) {
        $(this).text('Sending……');
        return true
    }
    else {
        e.preventDefault();
        e.stopImmediatePropagation();
        return false;
    }
} 
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Lawrence. I updated the question by deleting the unnecessary messy parts which somehow escaped my previous inspection. I also tried your code, but it just submits straight away without validating anything.
Well, that's not too surprising. I find this approach to be pretty inconsistent, but I've posted an alternative approach that is based upon preventing the default event if your validation fails. Give it another go.
Well, can I ask what expression you are using to test? There is another approach that is not fun, but have you at least checked to see that the function is being called? It might be helpful at this point to set up a jsfiddle
I got it worked eventually. The if conditions are not being met which caused the avalanche of troubles. I changed the condition to if (0<1) and the function is running. I will need to work on that part later. But thanks again for the help! Super helpful.
0

you can use a more easy option to validate your inputs check jquery validation engine ,an easy&powerful validation tool https://github.com/posabsolute/jQuery-Validation-Engine

1 Comment

Thank you. I would prefer stay on JS and JQuery though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.