Asp.net regex validator does not work - asp.net

My textbox should contain 8 digits. If not i want to pop up an alert box. But the regualr expression validator does not trigger. Why?
<asp:LinkButton ID="LinkPayment" CssClass="btn mainBtn" runat="server" onclick="LinkPayment_Click" OnClientClick="_gaq.push(['_trackPageview', '/virtualgoal/smspayment']);">Betal ></asp:LinkButton>
<asp:RequiredFieldValidator ID="RequiredCustomersPhone" Display="None" ValidationGroup="ValGroupCustomersPhone" ControlToValidate="CustomersPhone" runat="server" ErrorMessage="Du må fylle inn ditt mobilnummer!"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegExValCustomersPhone" ValidationGroup="ValGroupCustomersPhone" runat="server" ControlToValidate="CustomersPhone" Display="None" ErrorMessage="Telefonnummer må bestå av 8 siffer!" ValidationExpression="[0-9]{8}"></asp:RegularExpressionValidator>
<asp:ValidationSummary ID="ValidationSummary" ShowSummary="False" ValidationGroup="ValGroupCustomersPhone" ShowMessageBox="True" runat="server" />

Add the following code:
ValidationGroup="ValGroupCustomersPhone"
to your linkbutton

Are you trying to validate an empty field? You may need a required field validatior as well. It's a shame there is no 'ValidateEmptyText' field on Regex validators.

Related

asp:RequiredFieldValidator with telerik RadDatePicker

I using the asp:RequiredFieldValidator with telerik:RadDatePicker but the validation. style not appear ,this is my code:
<telerik:RadDatePicker RenderMode="Lightweight" EnableTyping="false" ID="RDPAddPremisesAndFixedListDate" runat="server">
<DateInput
DateFormat="dd MMM yyyy"
DisplayDateFormat="dd MMM yyyy"
CssClass="general-profile-Date">
</DateInput>
</telerik:RadDatePicker>
<asp:RequiredFieldValidator runat="server" ID="rfvPremisesAndFixedListDate" ControlToValidate="RDPAddPremisesAndFixedListDate"
ErrorMessage="This Field is Empty" ValidationGroup="AddPremises" CssClass="error-msg" />
This screenshot for my fields, all validation style appear except the (telerik:RadDatePicker), what is the problem?
I found the solution, I just added (extra-id="_dateInput_wrapper") in RequiredFieldValidator :)

Validator in asp.net

i have a textbox that will only accpet numbers, so i have to make validator that accept only number except 0, what's the regular expression i should do, any help please, i have already try this :
<asp:RegularExpressionValidator ID="regValSummary" runat="server"
ControlToValidate="txtSummary" Display="Dynamic" ValidationExpression="[1-9]*"
ValidationGroup="grpLigneComande">
*
</asp:RegularExpressionValidator>
any suggestion please !!
You can use Compare Validator instead of RegularExpressionValidator like
<asp:CompareValidator ID="CompareValidator1" runat="server" ValueToCompare="0" ControlToValidate="TextBox1"
ErrorMessage="Must enter positive integers" Operator="GreaterThan" Type="Integer"></asp:CompareValidator>
Hope this will help.
i think a RegularExpressionValidator won't be able to help you. Try exploring MaskedTextBox. C# Numeric Only TextBox Control
Or if you're using Ajax Control Toolkit, try the FilteredTextBoxExtender, using a sample MobileNo textfield which will accept numbers except 0:
<asp:TextBox ID="txtMobileNo" runat="server" CssClass="textBox" Width="200px" MaxLength="15" />
<ajax:FilteredTextBoxExtender runat="server" ID="FilteredTextBoxExtender4" TargetControlID="txtMobileNo" FilterMode="ValidChars" FilterType="Numbers, Custom" ValidChars="+-" InvalidChars="0" />

ASP.net RegularExpressions

Ok so I have a regular expression validator on my form but I am new to it and was wondering how I add another expression to it so I can look for something else as well. Here is my sample code:
<asp:RegularExpressionValidator
ID="RegularExpressionValidator3"
ControlToValidate="txtRelationship"
ValidationExpression="^[a-zA-Z''-,\'.'\s]{1,30}$"
EnableClientScript="true"
Display="None"
ValidationGroup="<%# ((TSAPassenger)((RepeaterItem)Container.Parent.Parent).DataItem).PaxKey %>"
runat="server"
ErrorMessage="Invalid Relationship.">
Invalid Relationship
</asp:RegularExpressionValidator>
I am trying to add in if the field name is equal to "test" or "tba" to give the same error or a different error. How would I add another expression into this?
Would the second validation look like this?
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="txtName" ValidationExpression="^.*\b(test|tba)\b.*$"
EnableClientScript="true" Display="None" ValidationGroup="<%# ((TSAPassenger)((RepeaterItem) Container.Parent.Parent).DataItem).PaxKey %>" runat="server" ErrorMessage="This can not be used as a Contact Name."></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorName" ControlToValidate="txtName" Enabled="<%# (Container.ItemIndex == 0 || Container. = "test") %>" ValidationGroup="<%# ((TSAPassenger)((RepeaterItem) Container.Parent.Parent).DataItem).PaxKey %>" runat="server" ErrorMessage="Contact Name is required."></asp:RequiredFieldValidator>
The best solution would be IMHO to use a custom validator and code your validations in the onServerValidate event.
Of course, you can always add another regExp validator or even a compare validator to the same object you are validating.
Hope this helps

force user select from autocomplete

i am beginner for asp.net . I now facing a problem . How can i force a user to select option from ajax control toolkit autocomplete?
Here is my sample code
<cus:cusTextBox ID="txtCMemberID" runat="server" Action="Edit"
CssClass="inputTextM" OnTextChanged="txtCMemberID_TextChanged"
AutoPostBack="True"></cus:cusTextBox>
<asp:AutoCompleteExtender ID="AutoCompleteExtender2" runat="server"
ServicePath="~/Module/Common/autoComplete/acLewreMember.asmx"
MinimumPrefixLength="1" ServiceMethod="GetSuggestedStrings"
TargetControlID="txtCMemberID" CompletionInterval="10"
CompletionSetCount="3" EnableCaching="true"
CompletionListCssClass="completionListElement"
CompletionListItemCssClass="listItem"
CompletionListHighlightedItemCssClass="highlightedListItem"
FirstRowSelected="True"
ShowOnlyCurrentWordInCompletionListItem="True">
</asp:AutoCompleteExtender>
As you see the textbox is a custom control. Is that any way to force user to select option from autocomplete?Please provide me a sample code also. thanks
ASP.NET includes a useful feature called a validator.
<asp:TextBox ID="TextBox2" ValidationGroup="Group2" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" ErrorMessage="*Required" ValidationGroup="Group2"
ControlToValidate="TextBox2" runat="server" />
<asp:Button ID="Button2" Text="Validate Group2"
ValidationGroup="Group2" runat="server" />
Here is the resource link for msdn: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.requiredfieldvalidator.aspx
I'm not sure how these work with custom controls as I'm relatively new at asp. They also have a custom validator which might be more suited for your purposes, but like I said. I don't know exactly.
Note: If the client is using an old browser this won't work, so you also want to check on the server that this field isn't empty by using:
if (Page.IsValid)
{
//stuff to do in your event handler
}

Regular expression for date in asp.net?

In web application, i am using regular expression for date like "dd-mm-yyyy" format for this i get the validation but it is not working when we enter date like "12-02-2012" remaining condition it is working fine, can you help me this is my validation expression.
<asp:RegularExpressionValidator ID ="myg" runat ="server" ControlToValidate ="txt" ErrorMessage ="Check"
ValidationExpression ="^(((0[1-9]|[12]\d|3[01])-(0[13578]|1[02])-((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)-(0[13456789]|1[012])-((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$" >
</asp:RegularExpressionValidator>
I solve the problem,
ValidationExpression ="^(((0[1-9]|[12]\d|3[01])-(0[13578]|1[02])-((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)-(0[123456789]|1[012])-((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$"
asp:RegularExpression is not the way to do this.
For validating date using an ASP.NET Validator its ideal to use asp:CompareValidator
Here is a small mock-up
<asp:TextBox ID="txtDatecompleted" runat="server"/>
<asp:CompareValidator ID="dateValidator" runat="server"
ControlToValidate="txtDatecompleted"
ErrorMessage="Please enter a valid date."
Operator="DataTypeCheck"
Type="Date"
ValidationGroup="TestVGroup">
</asp:CompareValidator>
<br />
<asp:Button ID="ClickButton" Text="Click Me" runat="server"
ValidationGroup="TestVGroup"
OnClick="ClickButton_Click" />
This will automatically check for different date formats and leap years
I slove the problem,
ValidationExpression ="^(((0[1-9]|[12]\d|3[01])-(0[13578]|1[02])-((19|[2-9]\d)\d{2}))|
((0[1-9]|[12]\d|30)-(0[123456789]|1[012])-((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])
\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|
((16|[2468][048]|[3579][26])00))))$"

Resources