javascript - jQuery Check user's input before form submission -
i'm trying validate user's input before submit form. although i'm successful doing when hit button, somehow couldn't make stop when press 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; } }
give 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; } }
Comments
Post a Comment