Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-21-2013, 08:01 PM   PM User | #1
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,461
Thanks: 52
Thanked 457 Times in 455 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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?
xelawho is offline   Reply With Quote
Old 01-21-2013, 08:13 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 01-21-2013, 08:22 PM   PM User | #3
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,461
Thanks: 52
Thanked 457 Times in 455 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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
xelawho is offline   Reply With Quote
Old 01-21-2013, 08:51 PM   PM User | #4
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,461
Thanks: 52
Thanked 457 Times in 455 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
weird. I just went back and timed it again, and now it's down to 300, without me doing anything
xelawho is offline   Reply With Quote
Old 01-22-2013, 07:46 AM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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?
__________________

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.

Last edited by Philip M; 01-22-2013 at 07:50 AM..
Philip M is offline   Reply With Quote
Old 01-22-2013, 09:00 AM   PM User | #6
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,553
Thanks: 9
Thanked 480 Times in 463 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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 5/13)
STATS (2013/5) HTML5:90.2% MOB:15.2% IE7:0.5% IE8:8.4% IE9:8.5% IE10:8.5%
rnd me is offline   Reply With Quote
Old 01-22-2013, 05:38 PM   PM User | #7
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,461
Thanks: 52
Thanked 457 Times in 455 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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.
xelawho is offline   Reply With Quote
Old 01-22-2013, 05:47 PM   PM User | #8
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 01-22-2013, 05:52 PM   PM User | #9
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 976
Thanks: 0
Thanked 203 Times in 198 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
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?
Logic Ali is online now   Reply With Quote
Old 01-22-2013, 06:28 PM   PM User | #10
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,461
Thanks: 52
Thanked 457 Times in 455 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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
xelawho is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:44 PM.


Advertisement
Log in to turn off these ads.