How to check form Validity by Javascript
You write a code for a login or signup form which is connected to
Database. So if you don't setup instant checking whether any field is
empty or not on click of submit/login/signup button, an empty value or
null value can be saved to your database. You can easily check the form
validity; means if there is any empty field or not by Javascript.
In the following code you can see I write a Javascript function within <head> and </head> tag. It takes the values of Username & Password field from the form on Submission. And then check the values whether it's empty or not empty. Thats it.
In the following code you can see I write a Javascript function within <head> and </head> tag. It takes the values of Username & Password field from the form on Submission. And then check the values whether it's empty or not empty. Thats it.
<html>
<head>
<script language="Javascript">
function formval()
{
var a = document.forms["form"] ["username"].value;
var b = document.forms["form"] ["pass"].value;
if(a == "" || a==null)
{
alert("Username field is Empty");
return false;
}
if(b == "" || b == null)
{
alert("Password field is Empty");
return false;
}
}
</script>
</head>
<body>
<div align="center"><form action="entry.php" name="form" onsubmit="return formval()">
Username: <input type="text" name="username" size="20">
Password: <input type="password" name="pass" size="20">
<input type="submit" name"submit" value="Submit">
</form></div>
</body>
</html>
Copy the above code and save it by any name with extension .html and run it on your browser and check whether it works or not.
Comments
Post a Comment