Validation to exclude SSN from textbox

Last post 01-04-2007 6:51 PM by julez. 4 replies.

Sort Posts:

  • Validation to exclude SSN from textbox

    01-03-2007, 7:34 PM
    • Loading...
    • julez
    • Joined on 12-02-2005, 5:57 PM
    • Posts 15

    Help! I need a validation control that will catch when users enter a social security number pattern in a textbox (with or without other content) and display its error message. Anyone have one or know how to use the standard controls to this effect? I've looked at RegExLibrary.com and have tinkered a little with the RegularExpressionValidator, but obviously don't understand them enough to figure this one out.

     A million thanks to whomever helps!!!!

     
    Jules
     

  • Re: Validation to exclude SSN from textbox

    01-03-2007, 8:43 PM
    Answer
    • Loading...
    • pkellner
    • Joined on 11-12-2004, 10:42 AM
    • San Jose, California
    • Posts 3,379
    • Moderator
      TrustedFriends-MVPs

    From our hero Scott 4 Guys.

     <input type="text" id="txtSSN" runat="server"/>
      </TD><TD>
        <asp:RequiredFieldValidator runat="server"
          id="reqSSN" ControlToValidate="txtSSN"
          ErrorMessage = "You must provide your Social Security Number."
          display="Dynamic" />
        <asp:RegularExpressionValidator runat="server"
          id="regexpSSN" ControlToValidate="txtSSN"
          ValidationExpression="^\d{3}\-\d{2}\-\d{4}$"
          ErrorMessage = "Your Social Seucrity Number must be in the format: XXX-XX-XXXX"
          Display="Dynamic" />
      </TD></TR>

    http://www.4guysfromrolla.com/webtech/090200-1.3.shtml

     

    Peter Kellner
    http://73rdstreet.com and blogging at
    http://PeterKellner.net
    MVP, ASP.NET
  • Re: Validation to exclude SSN from textbox

    01-03-2007, 8:51 PM
    Answer
    Hi, pls try the following regular expression.
    \w*\d{3}-\d{2}-\d{4}\w*
    Hope it helps.
  • Re: Validation to exclude SSN from textbox

    01-04-2007, 1:59 PM
    Answer
    • Loading...
    • PLBlum
    • Joined on 06-28-2002, 1:20 PM
    • Boston, MA
    • Posts 5,155
    • TrustedFriends-MVPs

    The challenge here is that you want to EXCLUDE the ssn. Regular expressions tend to require the pattern to be matched. You need a kind of "NOT" operator on the validator's result. That doesn't exist in the native RegularExpressionValidator.

    It means you have to go a different direction:

    1. Use a CustomValidator. You can still use the regular expression object from .net and javascript (http://www.synchro.net/docs/js/ref/regexp.html#1193136). After evaluating the SSN pattern (previously given as \W\d{3}-\d{3}-\d{4}\W), if its a match, set the IsValid value to false.

    2. My replacement to the native validators, Professional Validation And More, has a NOT operator on every validator, including its RegexValidator. So you can just do this:

    <vam:RegexValidator id="RV1" runat="server" ControlIDToEvaluate="TextBox1" ErrorMessage="No SSN"
       Expression="\W\d{3}-\d{3}-\d{4}\W" NotCondition="true" />

    --- Peter Blum
    Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
    www.PeterBlum.com
  • Re: Validation to exclude SSN from textbox

    01-04-2007, 6:51 PM
    • Loading...
    • julez
    • Joined on 12-02-2005, 5:57 PM
    • Posts 15

    Thank you!!!! You're suggestion was exactly what I needed. It's taken me an hour or two, but now I have a CustomValidator that works. Thanks!

     I've looked at your controls before. They're fabulous!

    Thanks again to all of you!

    Jules 

Page 1 of 1 (5 items)
Microsoft Communities
Page view counter