0

I need some help in regular expression. I am validating the textbox text when updating the records. When i click the update button, the first 5 letters should be equal to CM000 or cm000. How to validate this using regular expression in asp.net. does anyone know validationexpression for this. Let me know .

Thanks

1 Answer 1

1

You don't need a regex - do this instead:

bool isValid 
    = textBox.Text.StartsWith("CM000", StringComparison.OrdinalIgnoreCase);

If you must use a regex (like for a validation control) then use something like this:

<asp:RegularExpressionValidator 
    runat="server" 
    id="someId" 
    controltovalidate="textBox"
    validationexpression="^(?:CM|cm)000"
    errormessage="Invalid input" />
Sign up to request clarification or add additional context in comments.

4 Comments

do i need to use this in javascript?
@user356973 - It depends, how are you validating the control?
Nelson - Yes, it would but there is no need to capture if you are never going to use the result. But you are right, I don't need two groupings here - I will fix my answer.
Good point on not capturing; I always forget that exists. Also, just a quick note on RegularExpressionValidator is that it automatically adds the ^ and $ (start/end of text). See: stackoverflow.com/questions/678698/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.