what is the regular expression to validate a text box to allow "开发者_运维问答united states social security number" and also the text "- -" using regular expression validator.
ASP.net's Regular Expression Validator control has U.S. Social Security number as a selectable option.
(update: after googling, it seems that there are some requirements such as: first three digits must be 001 – 772. So I suggest you use the info on Google since it already presents a full tutorial on how it is done. The info below is just for a generic form).
not sure if asp.net uses a different regex syntax, but a general regex of SSN is
/^\d{3}-\d{2}-\d{4}$/
you may need to change it for asp.net.
the ^
is for beginning of string, $
is for the end, and \d
is for a single digit. The {3}
means it needs to repeat 3 times.
RegexBuddy suggests
\b(?!000)(?!666)(?:[0-6]\d{2}|7(?:[0-356]\d|7[012]))[- ](?!00)\d{2}[- ](?!0000)\d{4}\b
for a US social security number. Combining this with - -
into a .NET-friendly string gives you:
@"^(?:- -|(?!000)(?!666)(?:[0-6]\d{2}|7(?:[0-356]\d|7[012]))[- ](?!00)\d{2}[- ](?!0000)\d{4})$"
Do you also want to allow for variations in the - -
string? For example, more spaces?
精彩评论