Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Regular expression weirdness (Asp.net validator control)

Options
  • 31-05-2007 11:54am
    #1
    Registered Users Posts: 7,468 ✭✭✭


    ^(?=.*[a-z].*[a-z])(?=.*[A-Z].*[A-Z])(?=.*\d.*\d)[a-zA-Z0-9]{8,}$
    

    This checks that a string is at least 8 characters long, has 2 lower case character, 2 uppercase characters and two numeric characters. The above regex seems to be valid, at least it works in code. But when I apply it to a regex validator control it fails all the time. Does anybody have an idea why its not working? Suppose I'd better include the markup for the control ...

    [html]
    <asp:TextBox ID="txtDemo" runat="server"></asp:TextBox>
    <asp:RegularExpressionValidator ID="revDemo" runat="server" ErrorMessage="Failed"
    ValidationExpression="^(?=.*[a-z].*[a-z])(?=.*[A-Z].*[A-Z])(?=.*[0-9].*[0-9])[a-zA-Z0-9]{8,}$"
    ControlToValidate="txtDemo" EnableClientScript="true"></asp:RegularExpressionValidator>
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" CausesValidation="true" />
    [/html]


Comments

  • Moderators, Science, Health & Environment Moderators Posts: 8,849 Mod ✭✭✭✭mewso


    You say it fails but you don't say in what way. Could be a script problem. Does it work with enableclientscript=false?

    i.e. when the page post back and you call page.isvalid will it correctly show the error?


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Sorry, when I say it fails I mean the validation fails.

    Setting EnableClientScript to false worked but the problem is I need the clientscript working too. Any ideas?


  • Registered Users Posts: 437 ✭✭Spunj


    Evil Phil wrote:
    Setting EnableClientScript to false worked but the problem is I need the clientscript working too. Any ideas?

    That explains it. The RegEx works different on the client than the server. As far as I know, it uses a different syntax on the client (JScript I think). You will have to look up what's different in that syntax and modify the RegEx to match I reckon.

    edit:
    The JScript RegEx is a subset of the System.Text.RegularExpressions and its missing stuff like backreferences and other things that I forget right now. There has to be something in your RegEx thats not supported?


  • Moderators, Science, Health & Environment Moderators Posts: 8,849 Mod ✭✭✭✭mewso


    I'm up to here with Microsoft and their javascript. They haven't got a clue. i would suggest you implement your own solution.


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    I had a feeling it was going to be a javascript thing. I'll try getting it working and let you know how I get on.

    I'll probably just end up doing my own solution though.


  • Advertisement
Advertisement