Regular Expression

  • Thread starter Thread starter Jyoti Agarwal
  • Start date Start date
J

Jyoti Agarwal

Hi All,



I have a textfield which accepts a name. I am validating the text field
using the following statement



Regex.IsMatch(txtFirstName.Text, ("^[a-zA-Z\-\.]"))



The validation requirement is that it should not allow numbers or any
special characters. But the above regular expression still accepts special
characters and numbers.



Can you suggest the correct regular expression?



Thanks in advance



Jyoti
 
Hi Jyoti,

It is useful to *exactly* determine your validation requirement before
beginning to write a Regular expression.

In your case,

1. Do you know the MaxLength of the Text field ? You can include that
factor into the Regex.

2. Your expression "^[a-zA-Z\-\.]" will match a single character out of
the list(A-Z, a-z, ., -),
but *only a single* character. You must allow it to match the whole
text in the textbox (Unless of course, you only want to check if the
first character of entered text is Valid.)

For this you must modify your Regex to something like :

^[a-zA-Z\-\.]+

This will match one or more characters of the above list.

3. Finally, for ^ to work, you need to set the RegexOptions.MultiLine
option.

4. I do not see how even the Regex pattern you mentioned can match
special characters or numbers. Are you sure you are evaluating it
correctly ?

Hope this helps,

Regards,

Cerebrus.
 
Back
Top