I need a regular expression to validate whether text entered in an asp.net textbox has the following format
A-za-z123456789 /s = /s A-za-z123456789
Regular expression explained:
one or more alphanumeric characters
followed by any number of spaces
an equal sign
followed by any number of spaces
one or more alphanumeric characters
[a-zA-Z0-9]*\s*\=\s*[a-zA-Z0-9]*
Replace * with + if you want one or more rather than "any" (which includes zero)
Considering your answer to the comment about requiring one or more alphanumeric characters each side:
[a-zA-Z0-9]+\s*\=\s*[a-zA-Z0-9]+
This version will only match if there is at least one alphanumeric character each side of the "=".
If zero valid
"^[a-zA-Z\\d]+\\s*=\\s*[a-zA-Z\\d]+$"
If zero not valid
"^[a-zA-Z1-9]+\\s*=\\s*[a-zA-Z1-9]+$"
Related
I need to use an asp:RegularExpressionValidator control in my asp.net site and I want to restrict the characters to digits and semi-colon only.
So it needs to be able to take any amount of digits and ;
Valid values:
123
123;456
123;456;789;....
What is the regex for that?
Try this simple regex: [0-9;]+ or [\d;]+
\d match a digit [0-9]
; the literal character ;
+ means that it match between one character and unlimited times, as many times as possible,
If you want to guarantee that at least numbers are present in your expression you could do this too:
#npinti has a valid point better will be: ^[\d;]+$
where ^ indicates the begging of your expression and $ the end of it.
Online Demo
I am facing the problem in asp.net regular expression.
I need to validate if 5th or 6th character is "-" .
for example
3000-4567, 3000-4568 this string is , separated and also has a hyphen. I just need to check if each comma separated string has 5th or 6th character as a "-".
Current regular expression used in the system is
^((\s*\d{4,4}\s*[,]){1,3}?)?(\s*\d{4,4})*$
currently its validating 3000,4567
I've made two slight changes to your regex:
'^((\s*\d{4,5}\s*[/-]){1,3}?)?(\s*\d{4,4})*$'
Changed the cardinality of the first numeric group to {4,5} to allow for 5 digits numbers (which I guess is what you want since the dash can be the sixth character) and changed the separator to a dash. Notice the slash to escape it, since in square brackets the dash is a special character (tho' you probably don't need the brackets there).
As an alternative, consider splitting the string on instances of - and then validating the splitted chunks. That should be much easier.
The validator should allow only alphabetic characters (a-z and A-Z), dots(.),comma(,), slash(/) and hyphen (-). Please help to to find out one. Or tell me how to create one customized to my specifications.
I have tried [a-zA-Z,-/.] and it works but my requirements only allow for a maximum of 1 of each of the non-letter characters I specified (.,/-).
Try: ^[A-Za-z]*[-a-zA-Z,/.]{1}[A-Za-z]*$
Explanation
^ Anchor to start of string
[A-Za-z]* may be surrounded by multiple letters
[-a-zA-Z,/.]{1} Only one of the enclosed characters
[A-Za-z]* may be surrounded by multiple letters
$ Anchor to end of string
I have a problem with Regular Expression Validator. It takes 0-9, A-Z and prevents ' and " But it not takes lowercase alphabets. Here is my expression ^[a-z|A-Z|0-9|]+[^\"\']*$
You should use:
^[a-zA-Z0-9]+$
The | , "OR", should be used in groups ([a-z]|[A-Z|..). Also, by adding [^"']*, you allow users to enter phrases like a ##%$^&&&&*&^&*#$##$ (starting with alphanumeric character, followed by any non-quote char).
My suggested RegEx means:
^ <start>
[a-zA-Z0-9] Any alplhanumeric character, case-insensitive
+ at least once
$ <end>
If all you need is that the text contain exclusively letters and digits, you could use:
/^[a-zA-Z0-9]+$/
Notice that there is no | in the character set, all the things you put inside the [] are implicitly "or"ed together. Adding a | would allow for a literal | in the text.
Change the + to * if an empty string is valid. You don't need to exclude quotes specifically, since you're not allowing them in at all.
Shorter version of similar regexp:
^[\w]+$
Note: it will also match non-Latin characters and "_", which may be either good or bad depending on your requirements.
i have got a code to edit some function. There is a text box in that web application. It using a regular expression validator control to validate the text box. the validation expression is
ValidationExpression="[\w]{3,15}"
it accept all letters,numbers and underscores. but it do not accept special characters like \,/ * . i want to change the above regular expression to accept / .
i hope someone can explain what the above regular expression means and how to change that expression to accept / without affecting current regular expression
i am using asp.net and c#
string ValidationExpression= "[\w/]{3,15}"
[...] match a single character presents in the list between brackets
[...]{3,15} match between 3 and 15 characters presents between brackets
\w match a word character (letter, digit, underscore...)
/ match the character /
So [\w/]{3,15} match a word character or '/' between 3 and 15 times.
You current regular expression can be deconstructed as follows :
[] brackets represents regular expression group. Regex engine will try to match all the characters or group of characters given inside [] with the input string.
\w - Allow all the alpha numberic characters which includes upper case and lower case alphabets and 0 to 9 numbers and and underscore (This does not include other special characters like / or # ,etc ).
{3,15} means minimum 3 and maximum 15 alphanumeric characters must be provided in order to successfully match the string.
To add other charters, you need to add them explicitly. If you want to add / your regex should be like [\w/]{3,15}.
You can learn everything about regex here.