Friday, 22 January 2016

Validations using javascripting

Form Validation
 ¨No field should be left blank
¨ Email should contain asterik & dot or period symbols
¨Limit minimum length
¨Confirm Password
¨Restrict a field to accept Numeric or Alphabet


No field should be left blank.
var name = document.ContactForm.Name;
if (name.value == "")
    {
        window.alert("Please enter your name.");
        name.focus();
        return false;
    }
.................................................

Email should contain asterik & dot or period symbols
var email = document.ContactForm.Email;
if (email.value.indexOf("@", 0) < 0)
    {
        window.alert("Please enter a valid e-mail address.");
        email.focus();
        return false;
    }
    if (email.value.indexOf(".", 0) < 0)
    {
        window.alert("Please enter a valid e-mail address.");
        email.focus();
        return false;
    }


Complete Example

<script language="javascript">

function ValidateContactForm()

{
    var name = document.ContactForm.Name;
    var email = document.ContactForm.Email;
    var phone = document.ContactForm.Telephone;
    var comment = document.ContactForm.Comment;
    if (name.value == "")
    {
        window.alert("Please enter your name.");
        name.focus();
        return false;
    }
   
    if (email.value == "")
    {
        window.alert("Please enter your e-mail address.");
        email.focus();
        return false;
    }
   
if (email.value.indexOf("@", 0) < 0)
    {
        window.alert("Please enter a valid e-mail address.");
        email.focus();
        return false;
    }
    if (email.value.indexOf(".", 0) < 0)
    {
        window.alert("Please enter a valid e-mail address.");
        email.focus();
        return false;
    }
    if (comment.value == "")
    {
        window.alert("Please provide a detailed description or comment.");
        comment.focus();
        return false;
    }
    return true;
}
</script>

0 comments:

Post a Comment