I need one help i would like to perform simple validation in jquery but here the problem is that form is getting submitted every time even after giving error message please suggest me
in Getting Started
•
5 years ago
//// jquery code /////
$(function(){
function checkname(){
var letters = /^[A-Za-z]+$/;
var name = $("#idname").val();
if(!letters.test(name))
{
alert("only alphabets are allowed");
name.focus();
return false;
}
else
return true;
}
function checkmail(){
var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
var email = $("#idmail").val();
if(!mailformat.test(email))
{
alert("E-mail id is not valid");
email.focus();
return false;
}
else
return true;
}
function checkpwd(){
var pwd = $("#idpwd").val();
if(pwd.length<8)
{
alert("password should be atleast of 8 charachters");
pwd.focus();
return false;
}
else
return true;}
function checkrepwd(pwd){
var repwd = $("#idrepwd").val();
if(repwd!=pwd)
{
alert("password doesn't match");
repwd.focus();
return false;
}
else
return true;}
function checkcontact(){
var contact = $("#idcont").val();
if(contact.length<10)
{
alert("contact no is not valid");
contact.focus();
return false;
}
else
return true;}
function checkcomment(){
var comment = $("#idcomment").val();
if(comment=="")
{
alert("comment can't be blank");
comment.focus();
return false;
}
else
{
alert('Form Successfully Submitted');
return true;}}
$("#RegisterationForm").submit(function(){
if(checkname())
{
if(checkmail())
{
if(checkpwd())
{
if(checkrepwd($("#idpwd").val()))
{
if(checkcontact())
{
if(checkcomment())
{
return true;
}
}
}
}
}
}
else{
alert("form not submitted");
return false;
}
});
});
////HTML FORM CODING////////
<html>
<head>
<link rel="stylesheet" href="CSS1/stylesheet2.css"/>
<script type = "text/javascript" src = "jquery/jquery-3.2.1.js"></script>
<script src="jquery/validationjq.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>REGISTERATION FORM</title>
</head>
<body id="mybody1">
<form id="RegisterationForm" method="post" name="registrationform" action="processdata.php">
<center> <h1>REGISTRATION FORM</h1><table border="2" id="tbl1">
<tr><td>Enter name:</td><td><input type="text"name="txtname" id="idname"></input></td></tr>
<tr><td>Enter Email id</td><td><input type="text" name="txtemail" id="idmail"></input></td></tr>
<tr><td>Enter Password:</td> <td><input type="password" name="txtcls" id="idpwd"></input></td></tr>
<tr><td>Reenter Password:</td> <td><input type="password" name="txtrepass" id="idrepwd"></input></td></tr>
<tr><td>Enter Contact no:</td> <td><input type="text" name="txtcontact" id="idcont"></input></td></tr>
<tr><td>Enter Comments:</td> <td> <textarea name ="txtcomments" id="idcomment"></textarea></td></tr>
<tr align="center"><td rowspan="2"><input type="submit" value="Register"/></td>
<td rowspan="2"><input type="button" value="cancel"/></td></tr>
</table> </center></form>
</body>
</html>
1