Welcome to Home.

Wednesday, July 8, 2015

Form validation in JavaScript

It is most common task which is performed by using javaScript. We have likely come across forms on the web that have shown us a prompt when we haven’t entered a value into a field that requires one or when we have entered wrong kind of value, this is because the form has been validated i.e., a script has checked to see whether the text we have entered or choices we have made match. Some rules that the programmer has written into the page.


Example:
<html>
<head>
        <title>Form validation</title>
<script type=”text/javascript”>
Function validation()
{
        var a= document.form.name.value;
        Var b = document.form.name_1.value;
If(a==””&& b==””)
{
        alert(“ Enter your Name and password ”);
}
Else if(b==””)
{
        alert(“Enter your password”);
}
Else if(a==””)
{
        alert(“Enter your Name);
}
Else
{
        alert(“Form is filled successfully”);
}
}
</script>
</head>
<body>
        <form name=”form” method=”post” onsubmit=”validation()”>
        Name:<input type=”text” name=”name”>
        Password:<input type=”password” name=”name_1”>
<input type=”submit” value=”submit”>
</form>
</body>
</html>

No comments:

Post a Comment