C# .net asp need expression validating social security number without dashes

I am trying to validate a user input field for social security number that does not have or require dashes in it using .net asp 3.5 framwork validation controls and want to know what the validate expression would look like?  (using C#)
tcmmaxtAsked:
Who is Participating?
 
abelCommented:
> maybe you mean create an onclick method for my formview Insert link and do something like this?

Yes, I think so (without seeing your full code)

At some point, I assume you have some RegexValidator. Elsewhere, you probably have some input field. Then you have an Insert Link (as you say). That is a LinkButton I assume. A linkbutton has a CausesValidation property, which must be set to true. If so, you do not need to do anything. You can, at any given point (after Page_Load) use this.IsValid, that does not necessarily have to be inside the Click handler.

If you are unsure whether Validate is fired, you can call it by hand, using this.Validate().
0
 
abelCommented:
If you remove the dashes, you can validate it using the following regular expression:

\d{9}

which means: 9 digits.
0
 
spprivateCommented:
^(?!000)([0-6]\d{2}|7([0-6]\d|7[012])) ([ -])? (?!00)\d\d([ -|])? (?!0000)\d{4}$

U.S. social security numbers (SSN), within the range of numbers that have been currently allocated. Matches the pattern AAA-GG-SSSS, AAA GG SSSS, AAA-GG SSSS, AAA GG-SSSS, AAAGGSSSS, AAA-GGSSSS, AAAGG-SSSS, AAAGG SSSS or AAA GGSSSS. All zero in any one field is not allowed. ** Additionally, spaces and/or dashes and/or nothing are allowed.

Reference  RegExLib
http://regexlib.com/Search.aspx?k=social%20security
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
abelCommented:
nice work, spprivate, quite a bit more precise :). The only thing: it should be without dashes (see caption of question). That would become (and I think I removed a tiny error, too, plus your compulsary whitespace):

^(?!000)([0-6]\d{2}|7([0-6]\d|7[012]))( )?(?!00)\d\d( )?(?!0000)\d{4}$

and if you want to include those other rediculous invalid numbers, like 666:

^(?!000|666)([0-6]\d{2}|7([0-6]\d|7[012])) ?(?!00)\d\d ?(?!0000)\d{4}$

note also that the number 078-05-1120 is not allowed (wikipedia) and that 987 numbers can be promotional-allowed numbers, but they are disallowed by your expression already, because you include that 776 is the highest allowed.
0
 
abelCommented:
Now that we're on it, let's make it even better, based on wikipedia:
^(?!078 ?05 ?1120)(?!000|666|73[4-9]|74\d)([0-6]\d{2}|7[012]\d|73[0-3]|7[56]\d|77[012]) ?(?!00)\d\d ?(?!0000)\d{4}$

now the numbers starting with 734 would return false, 733 would return true, 749 would return false, but 750 again true. Anything above 772 will return false.

-- Abel --
0
 
spprivateCommented:
Yeah,I missed the part :)
0
 
tcmmaxtAuthor Commented:
great stuff & thank you experts.  before i close, I set my insert button "causes validation = true" , however my formview insert link still inserts new record even with a validation SSN error message? Something else I have to set or add? Not sure what's missings to prevent inserting a record on error?
0
 
abelCommented:
Use the following:

  if (this.IsValid())   {        //... do your update ...   }
-- Abel --
0
 
tcmmaxtAuthor Commented:
expert abel: thanks however the application uses a DAL tier and BLL layer and a objectdatasource with InsertMethod="addrecord" in FormView and i am not sure how or where to add above Is.Valid() ?
Maybe something like or similar to this  somewhere in code behind

 protected void MyInsertForm_ItemInserting(object sender, FormViewInsertEventArgs e)
 {
    if (this.IsValid())
    {
        objectdatasource .InsertMethod.addrecord  this cannot be right ?
    }
}
0
 
abelCommented:
The IsValid is a property (sorry, I said it is a method, but it is not) of the form and is available on any form. It will only return true if all validation controls in the current validation group have returned true. Don't forget to first call this.Validate if you have any custom validators or if you do not use CausesValidation property but still want to validate.

Use it like this:

protected void btn_Click(object sender, EventArgs e)
{
   if (this.IsValid)
   {
      // do something
   }
}
0
 
tcmmaxtAuthor Commented:
abel: again thanks for the help but I am using the FormView with an Insert link that triggers the  "addrecord" in  the  InsertMethod="addrecord"  which forms a part of  my objectdatasource like shown here:
<asp:ObjectDataSource ID="MyFormDataSource" runat="server"  etc.....
   InsertMethod="addrecord"   etc......

maybe you mean create an onclick method for my formview Insert link and do something like this?
In  the C# code behind:
 protected void InsertButton_Click(object sender, EventArgs e)
    {
        if (this.IsValid)
       {
          MyFormDataSource.InsertMethod="addrecord" ; // ? I don't if this code will prevent insert  
     }

    }
0
 
tcmmaxtAuthor Commented:
abel:  thanks. i had the insert link set to validate and it appearingly worked, everything is ok. I am curious if the std dot net RegexValidator that i using can somehow also popup a alert Javascript window?
0
 
abelCommented:
> curious if the std dot net RegexValidator that i using can somehow also popup a alert Javascript

probably, but I don't know 1-2-3 how to do that... maybe someone else?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.