How to allow double quotes in regular expression? - asp.net

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}$";

Related

asp.net regex for all A-Z a-Z numbers spaces and ! " ' but not accented characters

I am having a number of issues with my reg ex
<asp:TextBox ID="txtSmsMessage" Font-Names="" Font-Size="Small" runat="server" Width="450px" Name="txtSmsM" Height="150px" Style="margin-left: 50px; resize: none;" Value="Enter SMS" MaxLength="160" TextMode="MultiLine" onkeypress="return taLimit(this)" onkeyup="return taCount(this,'myCounter')" />
<asp:RegularExpressionValidator ID="x" runat="server" ErrorMessage="you have inputted incorect charicters" ControlToValidate="txtSmsMessage" ValidationExpression="#'^[a-zA-Z0-9\_]+$'" />
The current one #'^[a-zA-Z0-9\! will not find ! or spaces
Basically I need a reg ex for numbers letters and special characters but what to stop things like ë, the input box as you can see if for a sms message and character like ë error with our sms sending provider, thanks Mike
Try
^[a-zA-Z0-9!_ ]+$
Add the appropriate symbols inside the character set.
the best way is to use this ^[a-zA-Z0-9!_ ]+$ you needed to add the characters inside your []

How to filter our a specific email using Regularexpression validator

I'm writing a program which has a email field. I want to put a restriction that only username#gmail.com emails would be valid. But when I try to run, my RegularExpression Validator gives me a mistake
This is the code I'm using:
<asp:TextBox ID="ExistMail" runat="server" ></asp:TextBox><font size='2'>
<asp:RegularExpressionValidator ID="RegularExpressionValidator3" class="errorMess" runat="server" ErrorMessage="Invalid Email Address!!!" ControlToValidate="ExistMail"
<ValidationExpression="\w+([-+.']\w+)*#\w+gmail.com\w+)*"></asp:RegularExpressionValidator>
Remove \w+ after of # and \w+ in the end of string
\w+([-+.']\w+)*#gmail.com

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.

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" />

Regular expression validation (should start with character )

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"/>

Resources