• Related Post

    Friday, July 21, 2017

    Required Field Validation using ASP.Net | Java Script Required Field Validation | Required Field Validation java script code for contact form

    Hello Friends,

    As you know everybody need require field validation for their inquiry form or any other kind of forms. Because many people do not provide right information regarding their email id contact number etc. So today i am going to tell you how you can setup require field validation in your website form.

    I am going to provide many way how you can setup require field validation by using asp.net, java script etc.

    Require Field Validation using ASP.Net  

    If you are using Visual Studio then its become very easy to keep validation because Visual Studio provide you control tag.

    Source Code for Require Field Validation Message as popup form

    <asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="true" ShowSummary="false"/>
            <asp:Label ID="Label1" runat="server" Text="Name*"></asp:Label>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:requiredfieldvalidator ID="Requiredfieldvalidator1" runat="server" errormessage="enter your name" Display="None" ControlToValidate="TextBox1"></asp:requiredfieldvalidator>
            <asp:Button ID="Button1" runat="server" Text="Submit" />


    Screenshot

    require field validation message as popup form


    Source Code for Require Field Validation Message as Summery 

    <asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="false" ShowSummary="true"/>
            <asp:Label ID="Label1" runat="server" Text="Name*"></asp:Label>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:requiredfieldvalidator ID="Requiredfieldvalidator1" runat="server" errormessage="enter your name" Display="None" ControlToValidate="TextBox1"></asp:requiredfieldvalidator>
            <asp:Button ID="Button1" runat="server" Text="Submit" />

    Screenshot
    require field validation message as summery

    Java Script Code for Require Field Validation 

    Add following Java Script code in your webpage Head section

    <script>
            function validateForm() {
                var x = document.forms["validationForm"]["fname"].value;
                if (x == "") {
                    alert("Please Enter Your Name");
                    return false;
                }
            }
    </script>

    Use Following Code where you want to setup Validation

    <form name="validationForm" onsubmit="return validateForm()" method="post" action="">
    Name: <input type="text" name="fname">
    <input type="submit" value="Submit">

    Screenshot
    java script require field validation

    If you face any difficulty using these code or not able to setup validation in your contact form please write in comment box i will reply you as soon as possible

    1 comment: