<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="jquery-3.1.1.min.js"></script>
<script>
$(document).ready(function(){
$("#submit").click(function(){
var usernameReg=/^[a-zA-Z ]$/;
var mobnumReg=/^[0-9]$/;
var emailReg=/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
var errors=false;
if($('#username').val()==''){
$('#username').after('<span class="errors">missing name</span>');
errors=true;
}
if($('#mobnum').val()==''){
$('#mobnum').after('<span class="errors">missing number</span>');
errors=true;
}
if($('#mail').val()==''){
$('#mail').after('<span class="errors">missing mail</span>');
errors=true;
}
if($('#pwd').val()==''){
$('#pwd').after('<span class="errors">missing password</span>');
errors=true;
}
if($('#addr1').val()==''){
$('#addr1').after('<span class="errors">missing address</span>');
errors=true;
}
if($('#addr2').val()==''){
$('#addr2').after('<span class="errors">missing address</span>');
errors=true;
}
if(errors==true){
return false;
}else{
return true;
}
}); });
</script>
</head>
<body>
<form method="post" id="user_form">
NAME <br>
<input type="text" name="username" id="username" class="req" ><br>
MOBILE NUMBER <br>
<input type="text" name="mobnum" id="mobnum" class="req" maxlength="10"><br>
E-MAIL <br>
<input type="email" name="mail" id="mail" class="req" ><br>
PASSWORD <br>
<input type="password" name="pwd" id="pwd" class="req" ><br>
ADDRESS 1<br>
<input type="text" name="addr1" id="addr1" class="req" ><br>
ADDRESS 2<br>
<input type="text" name="addr2" id="addr2" class="req" ><br>
<button type="submit" id="submit">Submit</button>
</form>
</body>
</html>
hi.. i have written code for jquery form validation with regex... here error message displayed only for incorrect email..if i type incorrect user name and mobile number it should show error message .... how to add pattern for that ?
usernameReg,mobnumRegandemailRegvariables .. you need to apply them on the way of inputs.