Solved

Validate SSN data for all zeros or ones in text box

Posted on 2008-10-09
8
905 Views
Last Modified: 2012-05-05
Hi, I'm trying to figure out how to validate data that users enter into input fields on a web form.  Basically, the users would be entering SSN# into the system but the system should prevent them from entering all zeros or any of the same digits or sequential numbers.  For example, the system should validate if the users enter data as one of the following:

000-000-000
123-456-789
111-111-111
222-222-222
333-333-333
...so on

Please let me know if you can think of an efficient way of handling all these permutations.
Thanks a lot for your time and assistance.
0
Comment
Question by:LISCNY
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 22683437
is it canadian SSN? if this is the case, the last digit is a check digit and can be used to validate that the whole number is valid.
0
 

Author Comment

by:LISCNY
ID: 22683888
In this case, I'm referring to data in US.  Would you please elaborate what you mean?  
Basically, I need generic validation for any numeric data input into the system.  There should be a function that would check if the data entered in a text field contains all zeros, 1s, 2s, etc.  
There may be another function created that would check against the value entered in the field to prevent users from entering sequencial numbers.
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 22685373
In Canada, you can apply a formula to the first 8 digits of a SSN to validate the last one is valid. I don't know if US SSN are the same (a little Google search makes me think that it is not the same thing).
0
 
LVL 4

Accepted Solution

by:
brrrdog earned 300 total points
ID: 22690606
I would use a RegularExpressionValidator....

http://msdn.microsoft.com/en-us/library/eahwtc9e.aspx

Here's a place where you can get a couple of different regular expressions for a valid SSN:
http://regexlib.com/REDetails.aspx?regexp_id=535

I noticed the expression above does cover 000-000-0000, but not 555-555-5555.  A regular expression to do that would either need a lot of "ORs" or lookahead/lookbehind, neither of which is easy/clean.  So I would also use a CustomValidator in addition to the regular expression validator.  That way the regular expression validator will give you some client side validation for 99% of the cases and then the CustomValidator would let you write some very simple server side code to make sure all the digits aren't the same.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 14

Assisted Solution

by:raja_ind82
raja_ind82 earned 200 total points
ID: 22693429
Here is the simple javascript validation for SSN. is this you want?

Regards,
M.Raja
<HTML>

 <HEAD>

  <TITLE>M.Raja's Validation </TITLE>

      <script language ="javascript" >

    function validateSSN( strValue ) {

/************************************************

DESCRIPTION: Validates that a string contains a 

  valid social security number(XXX-XX-XXXX). 

  

 PARAMETERS:

   strValue - String to be tested for validity

   

RETURNS:

   True if valid, otherwise false.

*************************************************/

var objRegExp  = /^\d{3}\-\d{2}\-\d{4}$/;

 

  //check for valid SSN

  return objRegExp.test(strValue);
 

}

function isValidSSN(value) { 

    var re = /^([0-6]\d{2}|7[0-6]\d|77[0-2])([ \-]?)(\d{2})\2(\d{4})$/; 

    if (!re.test(value)) { return false; } 

    var temp = value; 

    if (value.indexOf("-") != -1) { temp = (value.split("-")).join(""); } 

    if (value.indexOf(" ") != -1) { temp = (value.split(" ")).join(""); } 

    if (temp.substring(0, 3) == "000") { return false; } 

    if (temp.substring(3, 5) == "00") { return false; } 

    if (temp.substring(5, 9) == "0000") { return false; } 

    return true; 

}
 

//function used to validate the inbox starts here

function  frm_validation()

{

	with(document.frm)

	{

		if((txtID.value)=="")

		{

			alert("Please enter your SSN");

			txtID.focus();

			return false;

		}

		else if(!(isValidSSN(txtID.value)))

		{

			alert("Not a valid SSN");

			txtID.focus();

			return false;

		}

		 alert("valid SSN");

	return true;

	}

}

    </script>

 </HEAD>
 

 <BODY>

<form name="frm" method="post">

  <input type="text" name="txtID">

  <input type="button" name="btnSubmit" onclick="Javacript: frm_validation();" value="ValidateSSN"><Br>

  Ex(123-45-6789)

</form>

 </BODY>

</HTML>

Open in new window

0
 
LVL 4

Expert Comment

by:brrrdog
ID: 22698095
raja_ind82's javascript validation is fine if you wanted to add that in ADDITION TO a custom validator so that the all cases of the SSN can be validated on the client side.  All validation should always be done on the server side (which is automatic with .net validators if you check Page.IsValid).  Supplementing (not substituting) javascript validation is done for performance and convience of the client.  But javascript validation is easily bypassed, so it should only be done when server side validation already exists.
0
 

Author Comment

by:LISCNY
ID: 22901721
Thank you all for participating in this question.  They were all good solutions but I have derived with the attached solution below that addresses my issue.
 
Private Sub btnValidate_Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnValidate_Save.Click
 

If CStr(Me.txtDUNCDC.Value).Trim().Length = 9 And ValidateNumericData(txtDUNCDC.Value) = False Then

                lblError.ForeColor = Color.Red

                Me.lblError.Text = "Incorrect Entry, please review and correct the data."

END If

END SUB
 
 

Protected Function ValidateNumericData(ByVal value As String) As Boolean

        Const seqNumber = "123456789"

        Dim lastDigit As String

        Dim countLen As Integer

        Dim compareData As String

        Dim i As Integer
 

        If value = String.Empty Then

            Return True

        Else

            countLen = value.Length

            lastDigit = value.Substring(countLen - 1)
 

            For i = 1 To countLen

                compareData += lastDigit

            Next

            If compareData.Length = 9 Then

                If value.Equals(compareData) Then

                    Return False

                ElseIf value.Equals(seqNumber) Then

                    Return False

                Else

                    Return True

                End If

            Else

                Return False

            End If

        End If

    End Function

Open in new window

0
 
LVL 4

Expert Comment

by:brrrdog
ID: 22902297
Just out of curiosity, are you using other .net validators (requiredfield, compare, etc) and the validationsummary control along with them?  If so is there  a reason you chose not to use a customvalidator?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now