0

As seen in Yahoo mail registration, whenever a field is left empty, it will prompt the user that the field is empty. How can I achieve this?

For example I have two asp textboxes; first name and last name, then I left the first name field empty and proceed with the last name field, it would prompt me that I left the first name field blank

thanks for your help.. Yahoo Mail inline validation example

2 Answers 2

1

You can use Required Field Validators for that. For example, if you have a textbox asking for the user to enter some name, you can follow the syntax :

<asp:TextBox runat="server" ID="TxtFirstName" CssClass="textEntry" Width="50%" MaxLength="100" AutoPostBack="false" onkeyup="javascript:shouldsubmit=false;" ValidationGroup="valEnquiry"></asp:TextBox>
                    <font color="red">*</font>
                    <asp:RequiredFieldValidator ID="TxtFirstName_RequiredFieldValidator" runat="server" ErrorMessage="First Name Required" ForeColor="Red" Font-Size="0.9em" ControlToValidate="TxtFirstName" Display="None"></asp:RequiredFieldValidator>
Sign up to request clarification or add additional context in comments.

Comments

0

You can use javascript for that.... Take this example

<script type="text/javascript">
function validateForm() 
{   
    var name=document.getElementById("<%=txtname.ClientID%>").value.trim();
    var contact=document.getElementById("<%=txt_contact_no.ClientID%>").value.trim();   
    var email=document.getElementById("<%=txt_email.ClientID%>").value.trim();
    var comments=document.getElementById("<%=txt_coments.ClientID%>").value.trim();

    if(name=="")
    {
        alert("Name must not be blank... ");
        document.getElementById("<%=txtname.ClientID%>").focus();
        return false;
    }
    if(email=="")
    {
        alert("Email Address must not be blank... ");
        document.getElementById("<%=txt_email.ClientID%>").focus();
        return false;
    }
    if(email != "")
    {
        var emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
        var compare=email.match(emailPat);    
        if(compare == null)
        {       
            alert("Invalid Email address ! Please try again!!");
            document.getElementById("<%=txt_email.ClientID %>").value ="";
            document.getElementById("<%=txt_email.ClientID %>").focus();
            return false;
        }

    }
    if(contact=="")
    {
        alert("Contact Number must not be blank...");
        document.getElementById("<%=txt_contact_no.ClientID%>").focus();
        return false;
    }

    if(comments=="")
    {
     alert("Comments must not be blank...");
     document.getElementById("<%=txt_coments.ClientID%>").focus();
     return false;
    }

    if(!isNaN(name))
    {
     alert("Numbers and special Characters are not allowed in Name field...");
     document.getElementById("<%=txtname.ClientID%>").focus();
     return false;
    }
    if(isNaN(contact))
    {
     alert("Only numbers are allowed...");
     document.getElementById("<%=txt_contact_no.ClientID%>").focus();
     return false;
    }

}
function textboxMultilineMaxNumber(txt,maxLen)
{
if(txt.value.length>50)
{
    return false;
}
}
function NumberOnly(txt,value)
    {
       var AsciiValue=event.keyCode
        if((AsciiValue>=48 && AsciiValue<=57) || (AsciiValue==8 || AsciiValue==127))
        {
            if(txt.value.lenghh>10)
            {
             return false;
            }
            event.returnValue=true;
         }
        else
            event.returnValue=false;

    }
 </script>

and put this script onClientClick of Button

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.