I have a textbox txtemailid which has two validator RequiredFieldValidator and RegularExpressionValidator as:
<asp:TextBox ID="txtEMailId" CssClass="Text1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RFVtxtEMailId"
runat="server"
ErrorMessage="*"
Display="Dynamic"
ForeColor="Red"
ControlToValidate="txtEMailId"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="REVtxtEMailId"
runat="server"
ValidationExpression="\w+([-+.]\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*"
Display="Dynamic"
ErrorMessage="Invalid Email Format"
ForeColor="Red"></asp:RegularExpressionValidator>
it shows following error
The ControlToValidate property of 'REVtxtEMailId' cannot be blank.
You not have specified which control should be validated .Try this
<asp:RegularExpressionValidator ID="REVtxtEMailId"
runat="server"
ValidationExpression="\w+([-+.]\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*"
Display="Dynamic"
ErrorMessage="Invalid Email Format"
ForeColor="Red" ControlToValidate="txtEMailId"></asp:RegularExpressionValidator>
You have not provided ControlTo Validate Property in Regular Expression Validator.Add this property in REVtxtEMailId
Related
I have a RadMaskedTextBox for SSN while entering value in it, the first two characters are clearing up itself and getting RegularExpressionValidator message. Can some body help me in this.This problem exits in IE browsers only.
<span id="SSN" runat="server">*</span>Social Security Number</label>
<asp:RegularExpressionValidator ID="TaxId_RegEx"
runat="server" ControlToValidate="TaxId"
Display="None" ValidationExpression="^\d{3}\-\d{2}\-\d{4}$" ErrorMessage="Please enter a valid 9 digit SSN." Enabled="true"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="TaxId_ReqField"
runat="server" ControlToValidate="TaxId"
Display="None" ErrorMessage="Please enter a value for this required field."></asp:RequiredFieldValidator>
<radI:RadMaskedTextBox CssClass="box_SSN" SelectionOnFocus="SelectAll" ID="TaxId"
Width="85px" Mask="###-##-####" runat="server">
</radI:RadMaskedTextBox>
Try:
^\d{3}-\d{2}-\d{4}$
Instead of:
^\d{3}\-\d{2}\-\d{4}
This is working perfectly for me in IE:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator1"
runat="server" ControlToValidate="TextBox1" Text="*">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator
ID="RegularExpressionValidator1"
runat="server"
ValidationExpression="\d{3}-\d{2}-\d{4}"
ControlToValidate="TextBox1"
ErrorMessage="Input valid SSN!">
</asp:RegularExpressionValidator>
<asp:Button ID="Button1" runat="server" Text="Submit"/>
Not sure why you have
Display="None"
My RangeValidator is working perfectly. My RequiredFieldValidator is not firing at all. Any ideas why?
<asp:TextBox ID="txtTestNumber" runat="server" MaxLength="4"
Text='<%# eval("LC") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvTestNumber" runat="server"
Display="Dynamic" EnableClientScript="False" ErrorMessage="Required"
ForeColor="Red" ControlToValidate="txtTestNumber" Enabled="true"
ValidationGroup="WizStepValidationGroup">required
</asp:RequiredFieldValidator>
<asp:RangeValidator ValidationGroup="WizStepValidationGroup"
Display="Dynamic" ID="rvTestNumber" runat="server" ErrorMessage="Invalid value"
MinimumValue="0" MaximumValue="9999" ControlToValidate="txtTestNumber"
Type="Integer"></asp:RangeValidator>
Thanks for your input!
Add the ValidationGroup="WizStepValidationGroup" property to the text box and the button.
UPDATE:
You have set EnableClientScript="False" of RequiredFieldValidator hence it is not getting validated at the client side. Hence remove the EnableClientScript property or set it to True and I am 100% sure then it will work.
I have search textbox,which has default value Enter Month to View Profit. When I click search button without entering any data, the default value of textbox is posted to server for search. I want that RegularExpressionValidator do not validate default value of textbox.
<asp:TextBox ID="Tboxsearch" Text="Enter Month to View Profit" OnClick="this.value=''" CssClass="textboxinput" runat="server"></asp:TextBox>
<asp:Button ID="ButtonSearch" CssClass="btnLog" runat="server" Text="Search" onclick="ButtonSearch_Click" />
<asp:RequiredFieldValidator
ID="RequiredFieldValidatorname"
runat="server"
ControlToValidate="Tboxsearch"
ForeColor="Red"
Text="*"
>
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator
ID="RegularExpressionValidatorname"
runat="server"
ValidationExpression="[a-zA-Z0-9]+"
ForeColor="Red"
ControlToValidate="Tboxsearch"
ErrorMessage="Enter Valid Name!"
>
</asp:RegularExpressionValidator>
The default for all validators except the RequiredFieldValidator control if you post with empty field the validator will not trigger
you must use required field validator with other validators to prevent the postback to happen
from MSDN
Special-Case Validation Results for ASP.NET Server Controls
EDIT
Also if you add your controls as in your question it should work but if there is other controls like for example other button you should set the validationGroup Property to the group that you want to work together
ValidationGroup="vGrp"
and your code will be like this
<asp:TextBox ID="Tboxsearch" Text="Enter Month to View Profit" OnClick="this.value=''" CssClass="textboxinput" runat="server" ValidationGroup="vGrp"></asp:TextBox>
<asp:Button ID="ButtonSearch" CssClass="btnLog" runat="server" Text="Search" onclick="ButtonSearch_Click" ValidationGroup="vGrp" />
<asp:RequiredFieldValidator
ID="RequiredFieldValidatorname"
runat="server"
ControlToValidate="Tboxsearch"
ForeColor="Red"
Text="*"
ValidationGroup="vGrp">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator
ID="RegularExpressionValidatorname"
runat="server"
ValidationExpression="[a-zA-Z0-9]+"
ForeColor="Red"
ControlToValidate="Tboxsearch"
ErrorMessage="Enter Valid Name!" ValidationGroup="vGrp"></asp:RegularExpressionValidator>
from MSDN about validationGroup
I'm trying to use two validators for the same control as shown in below code
<asp:TextBox runat="server" ID="tbEmail" placeholder="Enter email" class="form-control" required=""></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="tbEmail" ValidationGroup="DRF" Display="Dynamic"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="tbEmail" ValidationGroup="DRF" Display="Dynamic"></asp:RequiredFieldValidator>
But the problem is the second validator works but the first validator doesnot. I tried flipping them but same thing happens.
What I have to do to make both of them work at the same time ?
Can you help me in this?
Add an ErrorMessage attribute to the regex validator like so:
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="tbEmail" ValidationGroup="DRF" Display="Dynamic"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*" ErrorMessage="Please enter a valid email address"></asp:RegularExpressionValidator>
I want validation expression for required field validator and the numeric validator together
for phone number
<asp:TextBox ID="txtPhonenumber" runat="server" MaxLength="11" TabIndex="11" onKeyPress="return validateNumbersOnly();" ></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Enter Phone Number" ControlToValidate="txtPhonenumber" ></asp:RequiredFieldValidator>
this code works only if field is empty
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtPhonenumber" ErrorMessage="Only Numbers" Display="None" ValidationExpression="^\d+$" ></asp:RegularExpressionValidator>
ValidationExpression="^\d+$"
use this for numeric format validation
<asp:TextBox ID="txtPhonenumber" runat="server" MaxLength="11" ></asp:TextBox><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Enter Phone Number" ControlToValidate="txtPhonenumber" ></asp:RequiredFieldValidator><asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtPhonenumber" ErrorMessage="Only Numbers" Display="None" ValidationExpression="^\d+$" ></asp:RegularExpressionValidator>