0

I need to validate with javascript, a textbox with this format: 99/99999999/99 I've done a function that solves the problem. But I'd like to improved using Regex, but I'm new with regex. Any suggestion please?

This is how I maked before:

<asp:RegularExpressionValidator ID="RevNass" runat="server" 
ErrorMessage="NASS inválido. Formato correcto: ##/########/##" Display="None"
ValidationExpression="^\d{2}/\d{8}/\d{2}$"
ControlToValidate="TextBox1" meta:resourceKey="RevNass"/>

<asp:ValidatorCalloutExtender ID="RevNass_ValidatorCalloutExtender" 
runat="server" Enabled="True" TargetControlID="RevNass">
</asp:ValidatorCalloutExtender>

But I need to validated only with javascript in the onblur event, but i don't know how to convert this pattern "^\d{2}/\d{8}/\d{2}$" to javascript's pattern. I'm Sorry but this is urgent no time to readed a hugh tutorial, later i will.

1
  • Yes, have a look at regular-expressions.info. The expression that matches this pattern is not difficult and you should be able to create it after reading the basics. Commented Jul 11, 2011 at 9:47

2 Answers 2

2

/^[0-9]{2}\/[0-9]{8}\/[0-9]{2}$/ is a regex matching your pattern, assuming 9 can be any digit.

/^\d{2}\/\d{8}\/\d{2}$/ would work, too, but I usually prefer writing [0-9] as you immediately see what it's doing on a quick look at the regex.

Sign up to request clarification or add additional context in comments.

3 Comments

You should anchor the regex, unless it will match abc99/99999999/99def
Done, completely forgot that ;o
Thanks!! I finally do this: function myFuncion(tb) { var re = new RegExp(/^\d{2}\/\d{8}\/\d{2}$/); if (tb.value.match(re)) { tb.style.border = "1px solid #cccccc"; } else{ tb.style.border = "1px solid red"; tb.title = "NASS inválido"; } }
0

It's practically the same, you just need to enclose the regex in forward slashes

/^\d{2}\/\d{8}\/\d{2}$/

Comments

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.