I have the following textbox alongwith its validators, but when I enter the correct text it fires up the RegularExpressionValidator error message, can't really figure out what I am doing wrong
<asp:TextBox ID="txtName" runat="server" onkeypress="return keyRestrict(event,'abcdefghijklmnopqrstuvwxyz-0123456789')"
AutoCompleteType="Disabled" Width="199px"></asp:TextBox><span style="color: Red">*</span>
<asp:RequiredFieldValidator Display="Dynamic" ID="RequiredFieldValidator3" runat="server"
ControlToValidate="txtName"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revName" runat="server" ControlToValidate="txtName"
CssClass="text-error" Display="Dynamic" ValidationExpression="^[\p{L}\s]+$" ErrorMessage="Invalid Name"></asp:RegularExpressionValidator>
I also used the ValidationExpression ^[A-Za-z]+$ in order to test that whether there exist some error in the ValidationExpression, and then typed in Name in the textbox being validated and still it triggered the error message of the RegularExpressionValidator.
Regards
^[\p{L}\s]+$ would match 1 or more letters only(not digits) or space..
If you want to validate it with digits you have to use
^[\p{L}\p{N}\s]+$
This would match unicode letters,numbers and space 1 to many times
Related
I'm writing an application with multiple validators (date, required, regexp for format) on the same page. They validate correctly, but they turn up in the same place. That is, they don't turn up next to the validated field, they are appended to another validation error message next to a completely other field.
Here goes example code snippets:
Number:
<asp:RegularExpressionValidator class="IMSIValidators" runat="server" ErrorMessage="Enter digits only, no other characters" ControlToValidate="IMSIRangeTextBox" Display="Dynamic" ValidationGroup="AllValidators" ValidationExpression="[0-9]+"></asp:RegularExpressionValidator>
<p class="IMSIEditLabels">Operator: <asp:TextBox ID="OperatorTextBox" runat="server"></asp:TextBox></p>
<asp:RequiredFieldValidator runat="server" class="IMSIValidators" Text="Enter the customer" ControlToValidate="OperatorTextBox" Display="Dynamic" ValidationGroup="AllValidators"></asp:RequiredFieldValidator>
<p class="IMSIEditLabels">Requested by:<asp:TextBox ID="RequestedTextBox" class="IMSIEditInputItems" runat="server" ValidationGroup="AllValidators"></asp:TextBox></p>
<asp:RequiredFieldValidator runat="server" Text="Enter a full name" ControlToValidate="RequestedTextBox" Display="Dynamic"></asp:RequiredFieldValidator>
I've tried all ways that I can think of, such as setting Display="Dynamic" and ValidationGroup="AllValidators". No combination works for me.
I've tried to set their position in a CSS class, but then they came up on top of each other (which is logical).
Any input is greatly appreciated. I'm getting SO frustrated :)
//Eva-Lotta
I'm new to all this and trying to only allow numeric values to be entered into an existing TextBox in ASP.Net. I've got the code for this, but can't figure out where to put it in. If I do it like the below, it creates an additional box in the textbox (that works). How can I apply this limitation to the current textbox?
<asp:TableCell>
<asp:TextBox ID="txtS2" runat="server" Width="95%" AutoPostBack="true" OnTextChanged="WerklikChanged"></asp:TextBox>
<input type="text" onkeypress="filterDigits(event)"/>
</asp:TableCell>
Update:
I ended up using the below, which is not really what I wanted as it still only checks the field once I leave the field. I'll look into blocking the entry of letters completely at a later stage and post the answer here.
<asp:CompareValidator
ID="ProgrammaticID"
ControlToValidate="txtS2KoringA"
Type="Double"
Operator="DataTypeCheck"
ErrorMessage="Error Message"
Text=" Net nommers word toegelaat in die 'Werklik' velde"
ForeColor="Red"
BackColor="White"
SetFocusOnError="false"
Display="Dynamic"
runat="server">
</asp:CompareValidator>
I'm working on an ASP.Net application and I need to make sure my password field contains at least one special character.
This is what I have:
<asp:TextBox ID="Password" runat="server" TextMode="Password" />
<asp:RequiredFieldValidator ID="rfvPassword" runat="server" ForeColor="Red"
ErrorMessage="Password Required" ValidationGroup="vgErrors"
ControlToValidate="Password" Display="Dynamic" Text="*" />
Then I need to add an <asp:RegularExpressionValidator > to this field as well which needs to take the following special characters: (including a space)
!"#$%&'()*+,-./:;<=>?#[\]^_`{|}~
I'm struggling to implement this because for one, I'm not too familiar with the <asp:RegularExpressionValidator > control and I'm having some issues since they're special characters.
Any help would be much appreciated.
You can use the following regular expression to match a password containing between 8 and 16 characters, including one special character from your list. You can change at your need the range of total characters allowed:
^(?=.*\w)(?=.*[ !"#$%&'()*+,-./:;<=>?#[\]\^_`\{\|\}\~]).{8,16}$
In your aspx page, add a RegularExpressionValidator:
<asp:RegularExpressionValidator ID="revPassword" ControlToValidate="Password"
ForeColor="Red" ValidationGroup="vgErrors" runat="server" ErrorMessage="Error Message"/>
In the code-behind of your aspx page, you can set the ValidationExpression of your RegularExpressionValidator:
revPassword.ValidationExpression =
"^(?=.*\\w)(?=.*[ !\"#$%&'()*+,-./:;<=>?#[\\]\\^_`\\{\\|\\}\\~]).{8,16}$";
I don't really know if I should be using both the Regular Expression Validator and Required Field Validator or what? I have just a normal form in ASP.Net (Visual Studio). I want to make sure the user types in the correct characters, such as for a phone number (which is why I want the reg ex control), but I also want the field to be required. So should these be combined or what is the best way to do this?
Right now I just have the required part working, like this:
<asp:RequiredFieldValidator runat="server" id="RequiredFieldValidator2"
controltovalidate="TextBoxLocation" errormessage="Required"
/>
You can use both of them, but make sure to add Display="Dynamic" on both of the controls.
<asp:RequiredFieldValidator runat="server" id="RequiredFieldValidator1"
controltovalidate="TextBoxLocation" Display="Dynamic" errormessage="Required"
/>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Enter valid Phone number" ControlToValidate="TextBoxLocation" Display="Dynamic" ValidationExpression="^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$" ></asp:RegularExpressionValidator>
Both, simply because one validates client side and the other is server side.
This allows you to make sure the user didn't alter your clientside JavaScript and then send bad data.
I had been doing a type check for Double for an input field on a web page but now I need to allow commas. Can this be done using a CompareValidator or do I need to use a regex validator?
Rather than using Type="Double", try using Type="Currency". It should accept values with and without commas, however it will not accept more than 2 decimal places.
Here's an example:
<asp:TextBox runat="server" ID="TextBox1" />
<asp:CompareValidator runat="server" ID="cValidator" ControlToValidate="TextBox1"
Type="Currency" Operator="DataTypeCheck" EnableClientScript="true"
ErrorMessage="Invalid format!" Display="Dynamic" />
Otherwise a RegularExpressionValidator would work, coupled with a RequiredFieldValidator to validate empty entries (the regex validator doesn't prevent empty entries). You could use a CustomValidator, but you would need to come up with a client side validation routine in JavaScript if you don't want to rely solely on server side validation with a postback. Also, the client side solution may involve regex and it's more work to validate overall, although not too complicated.
This is an example using the RegularExpressionValidator:
<asp:TextBox runat="server" ID="TextBox1" />
<asp:RequiredFieldValidator runat="server" ID="rfValidator" Display="Dynamic"
ControlToValidate="TextBox1" ErrorMessage="Required!" />
<asp:RegularExpressionValidator ID="reValidator" runat="server"
ControlToValidate="TextBox1"
EnableClientScript="True"
ErrorMessage="Invalid Format!"
Display="Dynamic"
ValidationExpression="(\d{1,3}(,\d{3})*\.\d{2})|(\d+(\.\d{2})?)" />