Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,932
    Thanks
    56
    Thanked 552 Times in 549 Posts

    constructing a dynamic regex

    so here's the thing: I'm trying to write a code that dynamically constructs regexes to validate phone numbers depending on user input. there can be any number of formats, but the numbers will always be separated by hyphens. The user gets to choose the format, then later the validation runs according to what they have chosen.

    Thus:

    345-345435 could be valid
    as could
    2-44444444-44

    But I have been finding that constructing the regexes dynamically slows the code down noticeably. For example, if they're hardcoded like this:

    Code:
    var telformats = {
                        ph1: (/^(\d{4})-(\d{4})$/), //checks for format of phone number XXXX-XXXX (4 numbers dash 4 numbers)
                        ph2: (/^(\d{2})-(\d{4})-(\d{4})$/) //checks for format of phone number XX-XXXX-XXXX (2 numbers dash 4 numbers dash 4 numbers)
                    };
    it's almost instantaneous (around 300 millisecs to run the entire validation)

    but if I change it to this:

    Code:
    var inps = document.getElementsByTagName("input")
      for (var a = 0; a < inps.length; a++) {
                var str = this.value;
                var nums = str.split("-")
                var exp = "^"
                for (var i = 0; i < nums.length; i++) {
                    exp += "(\\d{" + nums[i].length + "})-"
                }
                exp = exp.slice(0, -1);
                exp += "$";
                var reg = new RegExp(exp)
    	telformats["tel"+a] = reg;
            }
    it slows it waaaay down, taking about a second longer to run the validation code.

    the only real difference that I can see is the use of the new RegExp constructor, but I'm assuming I have to use that because otherwise it's just a string. I even tried the dreaded eval(), but got the same results.

    Is there something else I can try?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Why not simply check for digits and hyphens?

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,932
    Thanks
    56
    Thanked 552 Times in 549 Posts
    because the validation has to follow the rules that the user dictates, so if they say for example that formats
    XXX-XXXX
    and
    XX-XXXX-XXX

    are valid, if they try to enter 22-343 it should fail validation

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,932
    Thanks
    56
    Thanked 552 Times in 549 Posts
    weird. I just went back and timed it again, and now it's down to 300, without me doing anything

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by xelawho View Post
    because the validation has to follow the rules that the user dictates, so if they say for example that formats
    XXX-XXXX
    and
    XX-XXXX-XXX

    are valid, if they try to enter 22-343 it should fail validation
    I am sure that you know what you are doing, but IMO excessive validation of telephone number formats is pointess when the user can simply make a mistake and enter his number wrongly, say with transposed digits. I would have thought that it would be enough to check for just digits and hyphens/spaces.

    Surely there are only a limited number of valid formats?
    Last edited by Philip M; 01-22-2013 at 07:50 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,302
    Thanks
    10
    Thanked 586 Times in 567 Posts
    using <input type=tel /> can eliminate entry errors on most browsers, especially on small-button mobile, because numbers are pasted in whole from a contact list instead of busy human beings.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #7
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,932
    Thanks
    56
    Thanked 552 Times in 549 Posts
    Quote Originally Posted by Philip M View Post
    I am sure that you know what you are doing...
    Surely there are only a limited number of valid formats?
    ha, ha. I wish I shared your confidence. Unfortunately, the formats range wildly, depending on which country the user is in - an Argentine cell phone could be:
    XX-XXXXXX or XX-XXX-XXXX or XX-XXXX-XXXX

    while a Vanuatu landline could be XX-XXX

    but you don't want an Argentine cell phone number to validate according to Vanuatu landline rules just because the user forgot a few digits, if you follow my thinking.

    Anyway. It's sorted now, but thanks for your input.

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by xelawho View Post
    ha, ha. I wish I shared your confidence. Unfortunately, the formats range wildly, depending on which country the user is in - an Argentine cell phone could be:
    XX-XXXXXX or XX-XXX-XXXX or XX-XXXX-XXXX

    while a Vanuatu landline could be XX-XXX

    but you don't want an Argentine cell phone number to validate according to Vanuatu landline rules just because the user forgot a few digits, if you follow my thinking.

    Anyway. It's sorted now, but thanks for your input.
    Surely it is up to the user to enter his telephone number correctly? Nothing will prevent digit transpositions etc. Likewise his email address. And indeed his name.

    In any case I never reveal my telepone number which is ex-directory. If a number is required in a form I just invent one such as 0207 270 3000 (which is David Cameron 10 Downing Street). Then he can deal with all the calls about ***** enlargement.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #9
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by xelawho View Post
    so if they say for example that formats
    XXX-XXXX
    and
    XX-XXXX-XXX

    are valid, if they try to enter 22-343 it should fail validation
    How do you validate that they chose the right format?

  • #10
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,932
    Thanks
    56
    Thanked 552 Times in 549 Posts
    Quote Originally Posted by Logic Ali View Post
    How do you validate that they chose the right format?
    that's up to them... I'm with Philip on that one - you can only go so far as to safeguard against human error


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •