<html>
<head>
<title>Html form validation</title>
<script type="text/javascript" src="java.js">
</script>
</head>
<body>
<center>
<form class="forms">
User Name * <input type="text" id="uname" placeholder="Username">
<br>
Password * <input type="password" id="pass" placeholder="password">
<br>
Cell No <input type="text" id="cell" placeholder="Cellno">
<br>
Email <input type="text" id="email" placeholder="Email">
<br>
Country
<select>
<option value="Nepal">Nepal</option>
<option value="USA">USA</option>
<option value="UK">UK</option>
<option value="China">China</option>
</select>
<br>
Sex
<input type="radio" name="sex" value="male">
<input type="radio" name="sex" value="female">
<input type="submit" onclick="formValidation()" value="submit">
</form>
</center>
</body>
</html>
<head>
<title>Html form validation</title>
<script type="text/javascript" src="java.js">
</script>
</head>
<body>
<center>
<form class="forms">
User Name * <input type="text" id="uname" placeholder="Username">
<br>
Password * <input type="password" id="pass" placeholder="password">
<br>
Cell No <input type="text" id="cell" placeholder="Cellno">
<br>
Email <input type="text" id="email" placeholder="Email">
<br>
Country
<select>
<option value="Nepal">Nepal</option>
<option value="USA">USA</option>
<option value="UK">UK</option>
<option value="China">China</option>
</select>
<br>
Sex
<input type="radio" name="sex" value="male">
<input type="radio" name="sex" value="female">
<input type="submit" onclick="formValidation()" value="submit">
</form>
</center>
</body>
</html>
Save this with form.html file
function formValidation()
{
var cell = document.getElementById("cell").value;
if(cell.length == 0)
{
alert("Enter cell");
document.getElementById("cell").focus();
}
var cellpat = /^[0-9]+$/;
if(cell.match(cellpat))
{ alert("valid entry");}
else
{
alert("Enter Valid cell");
document.getElementById("cell").focus();
}
}
{
var cell = document.getElementById("cell").value;
if(cell.length == 0)
{
alert("Enter cell");
document.getElementById("cell").focus();
}
var cellpat = /^[0-9]+$/;
if(cell.match(cellpat))
{ alert("valid entry");}
else
{
alert("Enter Valid cell");
document.getElementById("cell").focus();
}
}