Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Regular expression weirdness (Asp.net validator control)

  • 31-05-2007 11:54AM
    #1
    Registered Users, Registered Users 2 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: 9,213 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, Registered Users 2 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, Registered Users 2 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: 9,213 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, Registered Users 2 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