Regular expression validation (should start with character ) - asp.net

In my web application, I have a text box.
The user should enter a value of which the first character is a letter, not a digit, and the remaining characters can be alphanumeric.
How can I write regular expression to do this?

You can use: [A-Za-z]\w* to ensure the first character is a letter and any remaining characters are alphanumeric (optional by using the *)
<asp:RegularExpressionValidator ID="rev" runat="server"
ControlToValidate="txtBox"
ErrorMessage="First character must be a letter!"
ValidationExpression="[A-Za-z]\w*" />
<asp:RequiredFieldValidator ID="rfv" runat="server" ControlToValidate="txtBox"
ErrorMessage="Value can't be empty" />
The RequiredFieldValidator is used in conjunction with the RegularExpressionValidator to prevent blank entries. If that textbox is optional, and only needs to be validated when something is entered, then you don't have to use the RequiredFieldValidator.

<asp:TextBox id="TextBox1" runat="server"/>
<asp:RegularExpressionValidator
ControlToValidate="TextBox1"
ValidationExpression="^[A-Za-z]\w*"
ErrorMessage="Input must start with a letter"
runat="server"/>

Related

ASP: How to use a single span or div element to display validation errors

I have a field that required 3 different validators:
RequiredFieldValidator, RegularExpressionValidator, and CompareValidator.
Each validator has a "*" to show that something is wrong with the input:
<asp:TextBox ID="txtAddMin" runat="server" Width="30%" MaxLength="2"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ErrorMessage="Min Value is missing" ControlToValidate="txtAddMin" ForeColor="Red" ValidationGroup="vgBinAdd">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator8" runat="server" ErrorMessage="Invalid Min Value Entered" ControlToValidate="txtAddMin" ForeColor="Red" ValidationGroup="vgBinAdd" ValidationExpression="^[0-9]{2}$">*</asp:RegularExpressionValidator>
<asp:CompareValidator ID="CompareValidator2" runat="server" ControlToValidate="txtAddMin" ControlToCompare="txtAddMax" Operator="LessThan" Type="Integer" ErrorMessage="Min Value shoule be less then MaxValue" ForeColor="Red" ValidationGroup="vgBinAdd">*</asp:CompareValidator>
Validations work as expected, however, when failing, the "" is displayed in different positions. So, when CompareValidator fails, the "" is displayed a few spaces from the input field.
Basically, every validator takes up some space after the input field and "*" is displayed with extra space before it based which validator fails.
IS that possible to fix that, so, the start is displayed in the fix position no matter which validator fails?

Asp.net Range Validator

Html (Full Code)
<asp:TextBox ID="txtSasiNo" runat="server" Width="250px" MaxLength="17"></asp:TextBox>
<asp:RegularExpressionValidator CssClass="zorunlu" ValidationGroup="kaskoTeklifSayfasi" Display="Dynamic" ID="rangevalidator1" runat="server" SetFocusOnError="true" ControlToValidate="txtSasiNo" ErrorMessage="Enter number characters only (8 or 17)" ValidationExpression="^\d{8}$|^\d{17}$""></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="rfvSasiNo" SetFocusOnError="true" ValidationGroup="kaskoTeklifSayfasi" runat="server" Display="Dynamic" ControlToValidate="txtSasiNo" CssClass="zorunlu">Please enter Number.</asp:RequiredFieldValidator>
Question:
I am trying to use RangeValidator.
If txtnumber is null or empty and if txtnumber is not 8 characters or not 17 characters.
Only 2 options first option is 8 character second option is 17 character.
txtnumber text character only can be like below
12345678 // This is 8 characters
12345678901234567 // This is 17 characters
What to add to RangeValidator in order to achieve this ?
Thanks.
As described by Ondrej you can use RegularExpressionValidator. To be more specific with the aspx. See the code below:-
UPDATED CODE:
Here you go:-
<asp:TextBox ID="txtnumber" runat="server" Width="250px"></asp:TextBox>
<asp:RegularExpressionValidator Display="Dynamic" ID="regtxt" runat="server" SetFocusOnError="true" ControlToValidate="txtnumber" ErrorMessage="Enter number characters only (8 or 17)"
ValidationExpression="^\d{8}$|^\d{17}$"></asp:RegularExpressionValidator>
Hope it helps
Don't use range validator - use regex validator instead:
<asp:RegularExpressionValidator ValidationExpression="^([0-9]{8})|([0-9]{17})$" ... />
The validators usually ignore empty fields. Notable exception: the RequiredFieldValidator. Add that to guard against empty fields and keep the RangeValidator to force a specific range of values.

The server tag is not well formed?

I am getting the above error. I want to validate the textbox which allow some special characters but not letters and numbers. For that I wrote the below code but it is giving the above error.
The code
<asp:TextBox ID="txtTag" runat="server" Width="250"></asp:TextBox>
<asp:RegularExpressionValidator ID="reg1" runat="server"
ControlToValidate="txtTag" ErrorMessage="*" Text="please check"
ValidationExpression="/^[[\]'/\\# &(){}+$%#=~"-`/*.&]([[\]'/\\# &(){}+$%#=~"-`/*.&]*)$/" />
What am I doing wrong?
Escape the quotes in the ValidationExpression with ":
ValidationExpression="/^[[\]'/\\# &(){}+$%#=~"-/*.&]([[]'/\# &(){}+$%#=~"-/*.&]*)$/"
They collide with the outer quotes otherwise.
Use
/^[[\]'/\\# &(){}+$%#=~"-/*.&]([[]'/\# &(){}+$%#=~"-/*.&]*)$/
as ValidationExpression.

How to allow double quotes in regular expression?

I have a textbox :
<asp:RegularExpressionValidator ID="ValidateTitleCharacters" runat="server"
ValidationExpression="^[a-zA-Z0-9#+'.!#$',:;=/\(\),\-\s]{1,255}$"
ControlToValidate="title" Text="You have entered a character(s) that is not allowed in the title."
ErrorMessage="You have entered a character(s) that is not allowed in the title." />
I want to allow " character also. How can I modify the regular expression string???
I tried this:
<asp:RegularExpressionValidator ID="ValidateTitleCharacters" runat="server"
ValidationExpression="^[a-zA-Z0-9#+'.!#$'\",:;=/(),\-\s]{1,255}$"
ControlToValidate="title" Text="You have entered a character(s) that is not allowed in the title."
ErrorMessage="You have entered a character(s) that is not allowed in the title." />
<asp:RegularExpressionValidator ID="ValidateTitleCharacters" runat="server"
Validat‌​ionExpression="^[a-zA-Z0-9#+'.!#$',:;=/()(""),\-\s]{1,255}$"
ControlToValidate="title" Text="You have entered a character(s) that is not allowed in the title."
ErrorMessage="You have entered a character(s) that is not allowed in the title." />
Both attempts are breaking the string.
From the fragment you have posted, it appears that the regular expression is embedded in markup - this means you need to escape the double quote character as an HTML character entity.
Use ":
ValidationExpression="^[a-zA-Z0-9#+'.!#$'",:;=/\(\),\-\s]{1,255}$"
The ASP.NET engine will translate the character entity to ".
Alternatively, set the ValidationExpression value in code behind (in OnInit, for example):
ValidateTitleCharacters.ValidationExpression =
"^[a-zA-Z0-9#+'.!#$'\",:;=/\(\),\-\s]{1,255}$";

validation on textbox (no space)

i have a webpage in that i have a text box, my requirement is i don't want to give space in the textbox if user give space in textbox it give indication no space in the textbox
If you intended to capture a value where spaces aren't a valid character, you could use a RegularExpressionValidator:
<asp:RegularExpressionValidator ID="rev" runat="server" ControlToValidate="txtBox"
ErrorMessage="Spaces are not allowed!" ValidationExpression="[^\s]+" />
<asp:RequiredFieldValidator ID="rfv" runat="server" ControlToValidate="txtBox"
ErrorMessage="Value can't be empty" />
This would prevent "hello world" and "data base" since they contain spaces, and would only allow "helloworld" and "database" as valid values. You must use a RequiredFieldValidator in conjunction with it to prevent blank entries since the RegularExpressionValidator does not prevent that on its own.
Specify the name of the textbox in the ControlToValidate property.
You can use the RegularExpressionValidator control:
<asp:TextBox runat="server" ID="txt1" />
<asp:RegularExpressionValidator
runat="server" ErrorMessage="Spaces are not permitted"
ControlToValidate="txt1"
ValidationExpression="[^\s]+" />
The pattern [^\s]+ means "one or more characters that is not a space". So if any of the characters is a space, it will fail.
It might help you all
to remove your spaces in your document please try It.
<asp:TextBox runat="server" ID="txttitlename" />
<asp:RegularExpressionValidator runat="server" ErrorMessage="Spaces are not acceptable" ontrolToValidate="txttitlename" ValidationExpression="[^\s]+" />
You can use HTML page or Asp.Net page
only pattern="[^\s]+" in side TextBox
<input id="Text1" pattern="[^\s]+" type="text" />

Resources