Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello there,

I have a form I use and it works fine.

If a person fills in his phone number for example: 0525558887 which is the right format in my country, then ths data is submitted to my company's CRM.

I would like to implement a code that will act like pre-defined perfix on that certain field, for example: If a person will fill the form with 0525558887, the data that submitted will be processed as 972525558887 means cutting the '0' and adding 972


Any help or tip is appriciated.

Thanks.
Posted
Comments
Sergey Alexandrovich Kryukov 15-Jun-15 16:58pm    
Why would you need this 0? The number is the same. And do you understand why it happens? — it should be pretty apparent...
—SA

Please see my comment to the question. You can use, for example, http://stackoverflow.com/questions/10073699/pad-a-number-with-leading-zeros-in-javascript[^].

Alternatively, you can use one or another masked input control and remember the string itself, not just corresponding numeric value. See, for example, http://jsfiddle.net/SO_AMK/SEXAj[^].

—SA
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 15-Jun-15 17:27pm    
Perhaps, using a combobox (select) for the valid country codes (which that zero is converted into) and a textbox (input[type="text|number"]) would solve the riddle. Masked input is also a great way of solving such scenarios. See Solution 2 for that approach.

Also, +5 for masked input types.
Sergey Alexandrovich Kryukov 15-Jun-15 17:29pm    
Sure; thank you. Afzaal.
—SA
Well, my cell phone number is, 0332-*******, but if I had to give it to you I would have to strip out the first zero (leading zero) and add the country code to it, +92. Thus, you can contact me using my phone number, internationally. Of course I can send my cell number, and yes, in my own country the cell number if found to be starting with a zero.

For your scenario, a masked input would be a perfect fit (See the link[^] in Solution 1) because a script that uses the mechanism to prefix the characters is not efficient; believe me I tried it. What if the user doesn't enter the correct input, what if user already has added the zero, or what if he hasn't, or what if he enters the country code himself?

In such cases, I would (if I had to write the application) check a Combobox (select in HTML) control (In most of the software development frameworks), which holds the country codes for the phone numbers. Then, you can ask the user for his own number, without the country code. User would enter the number as, 332-******* (without the zero, or country code or other same stupid standard-based stuff). Then, I can append the +92 and store it in my database or process it. Same should be done here! It makes your life easy. ;)

Most of the giants do that, they ask you for your number without country code and give you a list of country names that you chose from and they append the country code to it.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 15-Jun-15 17:30pm    
Some good points here, a 5.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900