Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2009-04-17
13
Medium Priority
?
1,307 Views
Last Modified: 2013-11-08
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#)
0
Comment
Question by:tcmmaxt
  • 7
  • 4
  • 2
13 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24167916
If you remove the dashes, you can validate it using the following regular expression:

\d{9}

which means: 9 digits.
0
 
LVL 15

Assisted Solution

by:spprivate
spprivate earned 600 total points
ID: 24168945
^(?!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
 
LVL 39

Expert Comment

by:abel
ID: 24169066
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 39

Expert Comment

by:abel
ID: 24169238
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
 
LVL 15

Expert Comment

by:spprivate
ID: 24169258
Yeah,I missed the part :)
0
 

Author Comment

by:tcmmaxt
ID: 24169624
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
 
LVL 39

Expert Comment

by:abel
ID: 24170465
Use the following:

  if (this.IsValid())   {        //... do your update ...   }
-- Abel --
0
 

Author Comment

by:tcmmaxt
ID: 24171154
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
 
LVL 39

Expert Comment

by:abel
ID: 24171884
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
 

Author Comment

by:tcmmaxt
ID: 24172206
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
 
LVL 39

Accepted Solution

by:
abel earned 1400 total points
ID: 24172227
> 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
 

Author Comment

by:tcmmaxt
ID: 24184035
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
 
LVL 39

Expert Comment

by:abel
ID: 24186848
> 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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses
Course of the Month13 days, 11 hours left to enroll

581 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