protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
Regex regx = new Regex("^(?!666|000|9\\d{2})\\d{3}-(?!00)\\d{2}-(?!0{4})\\d{4}$");
Regex regx2 = new Regex("^(?!666|000|9\\d{2})\\d{3} (?!00)\\d{2} (?!0{4})\\d{4}$");
switch (regx.IsMatch(SSN.Text) || regx2.IsMatch(SSN.Text))
{
case false:
args.IsValid = false;
CustomValidator1.ErrorMessage = "Please enter correct SSN";
break;
case true:
args.IsValid = false; //Just for test, it should be--> args.IsValid = true;
CustomValidator1.ErrorMessage = "SSN correct";
break;
}
}
Result:
Hope this can help you.
Best regards,
Xudong Peng
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Jun 24, 2020 04:42 AM|anjaliagarwal5@yahoo.com|LINK
Hello,
Above Regex is checking if the SSN is valid or not. I just want any number that has nine digits and can be accepted with Hyphens and without hyphens. For e.g if someone enters
666542396
or
666-54-2396
They both should be valid. I came up with this, but it is accepting hyphens. I want a number with and without hyphens
^\d{3}-\d{2}-\d{4}$
the above number will fail with the Regex that you gave me.
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
Regex regx = new Regex("^\\d{9}$");
Regex regx2 = new Regex("^\\d{3}-\\d{2}-\\d{4}$");
switch (regx.IsMatch(SSN.Text) || regx2.IsMatch(SSN.Text))
{
case false:
args.IsValid = false;
CustomValidator1.ErrorMessage = "Please enter correct SSN";
break;
case true:
args.IsValid = false;
CustomValidator1.ErrorMessage = "SSN correct";
break;
}
}
Result:
More rigorous regular expressions:
Regex regx = new Regex("^(?!666|000|9\\d{2})\\d{3}-(?!00)\\d{2}-(?!0{4})\\d{4}$");
Regex regx2 = new Regex("^(?!666|000|9\\d{2})\\d{3}(?!00)\\d{2}(?!0{4})\\d{4}$");
Best regards,
Xudong Peng
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Does it have to be a Regex for some particular reason?
ssString.Where(c => char.IsDigit(c)).Count() == 9
Seems a simple way to determine if there are nine digits in a string. In my opinion the combination of IsDigit and == 9 makes it a lot easier to understand than what looks like swearing! #!/?#$
Member
511 Points
1273 Posts
social security number simple validation
Jun 24, 2020 01:08 AM|anjaliagarwal5@yahoo.com|LINK
Can someone help me with REGEX to use in regular Expression validator that accepts any social security number with or without hyphens.
any help will be highly appreciated
Contributor
2110 Points
674 Posts
Re: social security number simple validation
Jun 24, 2020 03:30 AM|XuDong Peng|LINK
Hi anjaliarwal5,
Based on your description, what you might want to do are:
If this is the case, I think you have to define a user control to achieve it. Please refer to below code:
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args) { Regex regx = new Regex("^(?!666|000|9\\d{2})\\d{3}-(?!00)\\d{2}-(?!0{4})\\d{4}$"); Regex regx2 = new Regex("^(?!666|000|9\\d{2})\\d{3} (?!00)\\d{2} (?!0{4})\\d{4}$"); switch (regx.IsMatch(SSN.Text) || regx2.IsMatch(SSN.Text)) { case false: args.IsValid = false; CustomValidator1.ErrorMessage = "Please enter correct SSN"; break; case true: args.IsValid = false; //Just for test, it should be--> args.IsValid = true; CustomValidator1.ErrorMessage = "SSN correct"; break; } }
Result:
Hope this can help you.
Best regards,
Xudong Peng
Member
511 Points
1273 Posts
Re: social security number simple validation
Jun 24, 2020 04:42 AM|anjaliagarwal5@yahoo.com|LINK
Hello,
Above Regex is checking if the SSN is valid or not. I just want any number that has nine digits and can be accepted with Hyphens and without hyphens. For e.g if someone enters
666542396
or
666-54-2396
They both should be valid. I came up with this, but it is accepting hyphens. I want a number with and without hyphens
the above number will fail with the Regex that you gave me.
Contributor
2110 Points
674 Posts
Re: social security number simple validation
Jun 24, 2020 05:29 AM|XuDong Peng|LINK
Hi anjaliagarwal5,
I may have misunderstood what you meant before.
As you say now, what you want might be like this:
Result:
More rigorous regular expressions:
Best regards,
Xudong Peng
Member
511 Points
1273 Posts
Re: social security number simple validation
Jun 24, 2020 10:20 PM|anjaliagarwal5@yahoo.com|LINK
Is their a way to combine both regex :
Participant
1620 Points
929 Posts
Re: social security number simple validation
Jun 24, 2020 10:57 PM|PaulTheSmith|LINK
Does it have to be a Regex for some particular reason?
ssString.Where(c => char.IsDigit(c)).Count() == 9
Seems a simple way to determine if there are nine digits in a string. In my opinion the combination of IsDigit and == 9 makes it a lot easier to understand than what looks like swearing! #!/?#$