Hi there! I often use the well known book by Franklin & Makar about Advanced AS2 Scripting. But, despite the fact they provide excellent samples of validation routines (email validation for example), they do not provide any example of validating sequences for phone numbers. In North America, we have a xxx xxx xxxx scheme. In France, it's xx xx xx xx xx. In Germany, it varies (!!!) : xxxxx xx xx or xxxx xxxxxxx or xxxxx xxxxx In Great-Britain, xx xxxx xxxx And so on... Will somebody could give me tips and syntax examples to write a validation function which will work for many different countries? (I know that this has something to do with "indexOf"...) I am not afraid to write as many as twenty different cases !!! :-) Many thanks in advance for your help !
Here's a simple way of validating: String.prototype.validate = function(test_str){ if(this.length!=test_str.length)return false; for(var i=0;i<this.length;i++){ if(this.charAt(i)=='#'){ if(!(test_str.charAt(i)>='0' && test_str.charAt(i)<='9'))return false; }else if(this.charAt(i)!=test_str.charAt(i)){ return false; } } return true; } en = '### ### ####'; trace(en.validate('123 456 7890')); trace(en.validate('123-456-7890')); trace(en.validate('1234567890')); trace(en.validate('456 7890')); trace(en.validate('123 4S6 7890')); Basically, it checks that the form length matches the test length. Then, if the form says there is a digit (#), it will make sure there is a digit in the test string. Otherwise, it expects exact character match between the two.
Thank you very much for this "speedy" answer! This is beautiful and, even if at first glance it appeared to me a bit complex (I am not a senior but in age!), I finally understood the "mechanics". I need some precisions: 1 - After en = '### ### ####'; all the trace statements stand for all the strings which are "acceptable". Am I right? 2 - I restricted the typing in the Input Field to only numbers and spaces ( no dash, dots, etc). So, I can remove, for example the statement trace(en.validate('123-456-7890'));? Am I right? 3 - The form will be used by aged persons, so I want to warn them as they are typing about their mistakes or, better, by clicking a Validate Button. In this case, how can I use the false and true returned in an Array of errors ( like error.push("bla bla bla") or something similar)?
Sorry for not explaining. All those traces are there just to shows examples of what it accepts and what it doesn't. In a real scenario you would have the code: String.prototype.validate = function(test_str){ if(this.length!=test_str.length)return false; for(var i=0;i<this.length;i++){ if(this.charAt(i)=='#'){ if(!(test_str.charAt(i)>='0' && test_str.charAt(i)<='9'))return false; }else if(this.charAt(i)!=test_str.charAt(i)){ return false; } } return true; } en = '### ### ####'; and then, here's how it would apply to a TextField. Suppose you have your button, validate_btn and phone_txt, and also info_txt which tells if the phone number is valid. You could use the following script: validate_btn.onPress = function(){ if(en.validate(phone_txt.text)){ info_txt.text = "Valid phone number entered!"; }else{ info_txt.text = "Please enter a number in the form: "+en; } }
[q][i]Originally posted by: [b][b]NSurveyor[/b][/b][/i] if(this.length!=test_str.length)return false; }else if(this.charAt(i)!=test_str.charAt(i)){[/q] Please, don't be sorry! Your valuable help is so important to me, it worth waiting a while for it! I tested your script within a separate file and... it works fine. I understood almost 95% of your code. But I don't like to simply copy and paste. I need to understand perfectly what I am doing. It's the only way to learn, isn't it? 1 - What exactly "this" stands for in the two lines I quoted? What will happen when I copy the code into my real file? Will "this" have its meaning changed? 2 - I am planning to use about a dozen of different schemes covering the phone numbers writing standards of the main countries in the world (examples: for North America na = '### ### ####'; for France fr = '## ## ## ## ##'; for Germany de_one = '##### ## ##'; or de_two = '#### ## ## ##';) How should I write the case switching? I want to thank you for all the time you are spending in solving my problem. Regards.
1. What I have written is a custom string method. "this" refers to the string that is calling the method. In my script, we use: en.validate(), therefore inside the method, "this" will refer to en. So the very first thing the script does is to check if the length of en (this) is the same as test string. The other line tests to see if the i-th character of both strings is the same. So, no changes are needed. 2. It all depends how you have the user select their country. I can help more with that with just a bit more explanation.
Okay, so suppose you have a number associated with each drop-down item, ie dd_val = 0 for first item selected, dd_val = 1 for second item selected etc. We could have an array of the different phone codes that correspond with the dropdown menu (North America would be first item on the dropdown, then France, etc.) codes = [na, fr, de_one, de_two]; Then to select the right code, use: sel_code = codes[dd_val]; Now, when you need to test, you can use something like: sel_code.validate(phone_txt.text) One more thing, for Germany, they should be able to enter either form, or is there some distinction made before hand?
Crystal clear, dear professor! Your question points to a problem I was just thinking about. And the answer is: no. User can't make a distinction (the form is to retrieve phone numbers and, of course, the user doesn't know how the phone he/she is looking for is formatted), so when Germany is selected, both versions de_one and de_two must be took in account...
In that case, you could instead have some array element be array of allowed codes, ie: [en,fr,[de_one,de_two]]. And then we would introduce another method, but this time for Arrays: Array.prototype.validate(test_str){ var v = false; for(var i=0;i<this.length;i++){ v = v || this[i].validate(test_str); } return v; }
[q][i]Originally posted by: [b][b]NSurveyor[/b][/b][/i] Array.prototype.validate(test_str){[/q] Okay. But the line quoted returns a Syntax error...
Whoops! Here is the correct code: Array.prototype.validate = function(test_str) { var v = false; for (var i = 0; i<this.length; i++) { v = v || this[i].validate(test_str); } return v; };
Don't see what you're looking for? Try a search.
|