Home » Asp.net

Required Field Validator Control

I have a question concerning the required field validator control. I have a form where I am using this control to validate that the user has inputed at leat something in the field. But I was wondering if I could use the same control for my own function that I want to use to do further validation once the user has inputed some data. For example in the case of providing an inoput field for email address. First Im test to see if they entered a valid value. Then I use a fuction I wrote to make sure the data is a valid email address. Thanks In Advance.


10 Answers Found


Answer 1

I dont think u can use the same required  field validator  for both..

For the same input control  u can use both required field  validator and Regular expression validator with which u can make sure that the email  address is valid  or not.  

Hope this helps..



Answer 3

make use of javascript onblur  function  and in that fuction  use regex to validate  that field  as email..


Answer 4


There are lot of possibilities by which you can achieve your requirement....

As Nishit mentioned CustomValidator is the best approach to make custom validation  (by using a function  to validate, both at clientside and serverside)....

And also there are other validation controls like RegularExpressionValidator (which canbe used to check for valid email  format string), CompareValidator (for checking the datatype)....

And if you got any reason to not to use the above mentioned Validator controls...then you can create your own CustomServerValidator by extending the BaseValidator class....

PS - In my opinion RequiredFieldValidator + CustomValidator would be sufficient to accomplish your requirement....

Hope this post helps you...


Answer 5

I assume you need to make sure the user  entering an email  in to a field,

use required  validator and regular expression validator... Using visual studio, you can set the validation  expression to internet email address.



Answer 6

This problem is occur becuase asp.net validator  is not work when we use textchanged event for textbox so.. it slip the vaildation . i also faced this problem and remember that .. it is problem with ajax ..  on that page you should remove updatepanel for those control.


Answer 7


Please try this

Suppose you have a TextBox (TextBox1) and a RequiredFieldValidator (RequiredFieldValidator1) and a Label (Label1) and a Button (Button1) .The Condithion is that:-> If the TextBox1 has some values then the text property of Label1 sets to "Textbox1 is Valid".

Use the SetFocusOnError property of RequiredFieldValidator1

 if (RequiredFieldValidator1.SetFocusOnError)
            Label1.Text = "Mahak";

protected void Button1_Click(object sender, EventArgs e)


           if (RequiredFieldValidator1.SetFocusOnError)


            Label1.Text = "Textbox1 is Valid";






Answer 8

Here is your solution :)

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>
    <script language="javascript" type="text/javascript">
        function  btnClick() {
            if (Page_ClientValidate() == true) 
                var sName = document.getElementById('txtName').value;
                alert('Thank you ' + sName + ' I feel validated!');
                //you can call any function which does your client side validation.
    <form id="form1" runat="server">
        <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" 
          runat="server" ErrorMessage="Please enter your name" 
          ControlToValidate="txtName">Please enter your name
        <input id="btnSubmit" type="button" value="Submit" 
           onclick="btnClick();" /></div>


Answer 9

I try your code but it show the compilation error on btnclick event. Can u try it and show me the o/p.

please suggest me.



Answer 10

You can Use First Require field  Validator For Compalasory Field
And For Validate Email you can Use Reqular Expression Validator


Samarth Patel



<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Sharepoint   |   Azure