473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Situational Phone Number Validation/Changing

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
3 2223
gits
5,390 Expert Mod 4TB
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
[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
5,390 Expert Mod 4TB
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

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

Similar topics

21
by: AnnMarie | last post by:
<script language="JavaScript" type="text/javascript"> <!-- function validate(theForm) { var validity = true; // assume valid if(frmComments.name.value=='' && validity == true) { alert('Your...
5
by: Kamaluokeakua | last post by:
I have to write an application that deals with clients in multiple countries. The addresses, phone numbers, country id and currency information has to be stored into a database that allows for the...
4
by: Srinivas Kollipara | last post by:
Hey guys, i have a small question in C#. I have a windows form which has Address fields, zip code and phone number fields. it is working fine with U.S. but i need to modify that for all the other...
2
by: Jeff Kiesel | last post by:
Has anyone used three textboxes for phone number input and successfully validated it? One textbox for area code, one for exchange, one for number. (yes, we're only doing US numbers) :o)
2
by: Sharon Traineanu via .NET 247 | last post by:
I'm using C#.net. I have a phone number in three asp textboxes. I'd like to make the phone number required, and validate to make sure only numbers have been entered. My main problem is I can't...
10
by: JackM | last post by:
I'm still working on validating the phone numbers that are entered on a form but have come across a problem I don't understand how to fix. I can handle most instances when it's in regular US...
6
by: Michael R | last post by:
I haven't found anything that would help me to understand the correct syntax for having a certain number of digits phone number validation rule. My input mask for this field is: \000\-0000000;;...
2
by: komaladevi | last post by:
hello all ! Can any one help me in validating US Phone Number , I wrotethe javascript for this to get the phone number as 123-234-1234 but i dont know the reason why i am getting this format only...
2
by: ajaymohank | last post by:
Hello friends............ i am trying to get a validation for email and phone number. in my php project i am doing the validation using java script. but i am not getting validations for email and...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.