Web Design & Development
[ Web Design & Development Topics ]
Form validation is the process of ensuring the end user completed all of the form fields in a format acceptable to the system. It is commonly done in JavaScript although can also be done on the server side using programs such as PHP. In general the form page will pop up an alert message if the user attempts to submit the form without completing all necessary fields.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>demo_form_validation.html</title>
<script type="text/javascript">
function check_form() {
if (document.f.lastname.value.trim()=="")
{alert("last name");
return false}
}
String.prototype.trim=function(){
return this.replace(/^\s+|\s+$/g,'');
}
</script>
</head>
<body>
<form method="post" name="f" onSubmit="return check_form()">
Last Name: <input type="text" name="lastname" />
<input type="submit" value="Submit">
</form>
</body>
</html>
Note: You can use other languages such as ASP or PHP to do the validation, but it is common to use JavaScript on the client side for this task.