By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,857 Members | 1,816 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,857 IT Pros & Developers. It's quick & easy.

Situational Phone Number Validation/Changing

P: 23
I am wanting to remove 1's that Canadian customers put in front of their numbers because when FedEx imports them for shipping they leave the 1 and cut off the last number. Other countries numbers don't get formatted the same so they are not an issue.

The code below was an attempt to remove a one at the beginning if present at all. Which should get the job done but I don't know if there are countries out there that need to start in the one.

The best result would be choosing Canada and then having a 1 as the first number when leaving the input field will cause the 1 to be deleted and a message displaying to the side why the one was removed so customers don't get confused.

Also as an added bonus if they choose Canada in the select field then the input field for state/province will turn into a select only listing Canadian Provinces. But the other is more important as it causes problems when fedex doesn't get the customers full phone number.

I
Expand|Select|Wrap|Line Numbers
  1. function Left(str, n){
  2.     if (n <= 0)
  3.         return "";
  4.     else if (n > String(str).length)
  5.         return str;
  6.     else
  7.         return String(str).substring(0,n);
  8. }
  9. function Right(str, n){
  10.     if (n <= 0)
  11.        return "";
  12.     else if (n > String(str).length)
  13.        return str;
  14.     else {
  15.        var iLen = String(str).length;
  16.        return String(str).substring(iLen, iLen - n);
  17.     }
  18. }
  19.  
  20. function canadaValidate(){
  21.     if (left(caPhone, 1) = 1)
  22.         caPhone = right(caPhone, 10);
  23.         document.main.SHIPPHONE = caPhone;
  24. }
  25.  
  26.  
  27. truncated form country select ->
  28.  
  29. <select name=''country>
  30. <option value='' SELECTED> SELECT </option>
  31. <option value='CA'>Canada</option>
  32.  
  33.  
  34. form input for phone number ->
  35.  
  36. <input NAME="phone" VALUE="" size="30" maxlength="30" onblur="canadaValidate(phone);">
  37.  
The right and left functions are not my own. I am not very familiar with a lot of javascript. so you can talk to me like I'm dumb...Thanks for you any help.
Dec 20 '07 #1
Share this Question
Share on Google+
3 Replies


gits
Expert Mod 5K+
P: 5,379
hi ...

i changed the code to fix and simplify the code ... have a look at the following example that might help you:

[HTML]<script type="text/javascript">
function validate_phone_no(node) {
var selected_country = document.getElementById('country_list');

switch (selected_country.value) {
case 'CA':
handle_canada(node);
break;
default:
// every country that needs no handlig aka. case-statement
// above
break;
}
}

function handle_canada(node) {
// we define a regExp that checks for a leading 1
var re = /^1/;

// message area
var msg_area = document.getElementById('msg_area');
var msg_txt = 'removed leading 1';
var val = re.test(node.value);

msg_area.innerHTML = val ? msg_txt : '';
node.value = node.value.replace(re, '');

return val;
}
</script>

<select name="country" id="country_list">
<option value='' selected>SELECT</option>
<option value='CA'>Canada</option>
<option value='DE'>Germany</option>
</select>

<input name="phone" value="" size="30" maxlength="30" onchange="validate_phone_no(this);">
<div id="msg_area" style="color: red;"></div>
[/HTML]
kind regards
Dec 20 '07 #2

P: 23
[HTML]
function handle_canada(node) {
// we define a regExp that checks for a leading 1
var re = /^1/;

// message area
var msg_area = document.getElementById('msg_area');
var msg_txt = 'removed leading 1';
var val = re.test(node.value);

msg_area.innerHTML = val ? msg_txt : '';
node.value = node.value.replace(re, '');

return val;
}

[/HTML]
kind regards
First off thanks for replying, this code takes care of the validation perfectly!!

Second if you could teach me to fish a little bit here, in the variable declaration on 're' you have it = /^1/ if you would be so kind to explain what that does and/or what I would search the technique by in say Google if I wanted to see it in other uses, hopefully in the future I would be able to use it in other code without having to ask again.

Thanks for your help!!
Dec 26 '07 #3

gits
Expert Mod 5K+
P: 5,379
hi ...

that is a regular Expression that i declared literally through:

Expand|Select|Wrap|Line Numbers
  1. var re = //;
  2.  
now we want to start at the beginning of the value that we want to test and for the character '1':

Expand|Select|Wrap|Line Numbers
  1. var re = /^1/;
for further information on regExp you may have a look here

kind regards
Dec 26 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.