Link to home
Start Free TrialLog in
Avatar of MichaelVB
MichaelVB

asked on

(ASP.NET) ValidationExpression needed.

Hi,

I need a ValidationExpression for a RegularExpressionValidator. Value must be 16 characters in length.

First character must be a letter a thru z or A thru Z    (first letter of last name)
Second character must be a letter a thru z or A thru Z  (first letter of first name)
Third chracter must be a letter m or f or M or F   (Male or Female)
Next Eight characters will hold DOB value, i.e.,  05031955 or 12211970  (DOB)
Character number 12 must be a hyphen     (-)
Last 4 characters will be last for digits of SSN  (1234)

Here is an example, SMM05031955-1234

If someone's got the time, I'd really appreciate your help.

Thanks,
Mike



Avatar of GwynforWeb
GwynforWeb
Flag of Canada image

This regular expression does it

/[A-Za-z]{2}[MF]\d{8}-\d{4}$/
ASKER CERTIFIED SOLUTION
Avatar of GwynforWeb
GwynforWeb
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MichaelVB
MichaelVB

ASKER

GwynforWeb,

Hey, thanks for the input. However,  this doesn't work. I am trying to input this value and RegularExpressionValidator does not allow it:
   MSM05181966-1234

Any suggestions?

Thanks again.
Mike
Hello Mike,
GwynforWeb's comment is perfectly OK. Problems may occur if you don't clear your browser's cache before loading the page again...
Anyway, here's a page that works fine with Gwyn's solution:

<% @Page Language="C#" Debug="true" %>
<script runat="server">

void Page_Load (object sender, EventArgs e)
{
  if(IsPostBack)
      {
        Validate();
        if(!IsValid)
        lab.Text="Wrong input format!";
      }
}

</script>
<html>
<body>
<form runat="server">
<ASP:TextBox runat="server" id="tb"/>
<ASP:RegularExpressionValidator runat="server" id ="regex"
         ControlToValidate="tb"
         ValidationExpression="^[A-Za-z]{2}[MF]\d{8}-\d{4}$"/>
<input type="submit"/><br/>
<ASP:Label runat="server" id="lab"/>
</form>
</body>
</html>
Hello Mike,
without ASP Label:
<% @Page Language="C#" Debug="true" %>
<script runat="server">

void Page_Load (object o, EventArgs e)
{
  if(IsPostBack)
      {
        Validate();
      }
}

</script>
<form runat="server">
<ASP:TextBox runat="server" id="tb"/>
<ASP:RegularExpressionValidator runat="server" id ="regex"
         ControlToValidate="tb"
         ValidatorDisplay="Dynamic"
       ValidationExpression="^[A-Za-z]{2}[MF]\d{8}-\d{4}$">
         Wrong input format!
</ASP:RegularExpressionValidator>
<br/>
             <input type="submit"/><br/>

</form>

Does this cover m or f along with M or F?

I noticed that GwynforWeb's solution contains forward slashes as opposed to quotes. Will this make a difference?

Thanks for your help everyone.
GwynforWeb,

Thanks for your assistance.

Mike