I want to perform a form validation with jQuery. Right now I'm using HTML5 'required' inside the input tag, and this code of jQuery for the validation:
jQuery:
$("#register").click(function() {
$("#register input").each(function() {
$(this).css('border','none');
if(!$(this).val()) {
$(this).css('border','solid 1px red');
}
var email_reg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
if($(this).attr("type")=="email" && !email_reg.test($(this).val())){
$(this).css('border','solid 1px red');
}
});
When using IE9, there is no support in HTML5 'required', so although the script is working, it's still running the input action .php file. I'll be happy to know can I fix it using the existing code and without using validate.js or other external files.
HTML5:
<form id="register" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post">
<p><label>First name</label><input type="text" id="name" name="name" required="true" /></p>
<p><label>Last name</label><input type="text" id="fname" name="fname" required="true" /></p>
<p><label>Email</label><input type="email" id="email" name="email" required="true" /></p>
<p>
<label>Phone</label>
<input id="area-code" type="text" name="code" maxlength="3" required="true" onkeydown="return isNumber(event);" />
<input id="phone" type="text" name="phone" required="true" maxlength="17" onkeydown="return isNumber(event);" />
</p>
<input type="submit" value="GET BONUS NOW" id="submit-btn" />
<?php include 'form.php';?>