473,735 Members | 2,945 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Javascript to Validate the Phone Number Field Using Javascript :-)

Hi this is my another validator in javascript to validate the Phone
Number :-)
<script language='javas cript'>
function funcCheckPhoneN umber(ctrtxtMob ile,e){
if(window.event ){
var strkeyIE = e.keyCode
if(((strkeyIE >= 48) && (strkeyIE <= 57 )) || (strkeyIE >= 40) &&
(strkeyIE <= 41 ) || (strkeyIE == 32) || (strkeyIE == 46)||(strkeyIE
== 45) ){}
else{
return false;}}
else{
var strkeyCode = e.keyCode
var strCharCode = e.charCode
if(((strCharCod e >= 48) && (strCharCode <= 57 )) || ((strCharCode
>= 40) && (strCharCode <= 41 )) || (strCharCode == 45) ||
(strCharCode==3 2)|| (strkeyCode==37 )|| (strkeyCode==38 ) ||
(strkeyCode == 46)||(strCharCo de==46)||(strke yCode==8 ) || (strkeyCode
==9 ) ||(strkeyCode== 39) || (strkeyCode ==35) || (strkeyCode ==36) ||
(strkeyCode==9) ){}
else{
return false;}}
return true;}

function valFuncReg_Phon eNumber(text,re g){
if(text == null || text == '')return true;
if(reg == null || reg =='')return true;
var regex = new RegExp(reg);var value=text;
var res= (regex.exec(tex t));
if(res==null){
reg =/^\d*$/;
regex = new RegExp(reg);
res= (regex.exec(tex t));
}
return (res != null && value == res[0]);}
</script>

The above script can be called on "Onkeypress " event of the TextBox as
follows :

"Onkeypress ", "javascript:ret urn funcCheckPhoneN umber(this,even t);"
Please feel free to give Comments and Suggessions and Bugs also :-)
Aug 5 '08 #1
5 3682
Abhishek wrote:
Hi this is my another validator in javascript to validate the Phone
Number :-)
[...]
Please feel free to give Comments and Suggessions and Bugs also :-)
Please stop posting your clueless, invalid junk code (here). Thanks in advance.
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Aug 5 '08 #2
On Aug 5, 4:33*pm, Abhishek <smartwebco...@ gmail.comwrote:
Hi this is my another validator in javascript to validate the Phone
Number :-)
It is much more efficient and a lot less code to use a regular
expression, something like:

function isPhoneChars(n) {
return /^[0-9 ()]*$/.test(n);
}

Forms are much more usable if you do not prevent input if errors are
detected, warn users of errors and let them fix it themselves, e.g.
<script type='text/javascript'>

function isPhoneChars(n) {
return /^[0-9 ()]*$/.test(n);
}

function validate(el) {
if (el.id == 'phone') {
var errEl = document.getEle mentById('phone _ErroMessage');
if (!isPhoneChars( el.value)) {
errEl.style.vis ibility = 'visible';
} else {
errEl.style.vis ibility = 'hidden';
}
}
}

</script>
<input type="text" id="phone" onkeyup="valida te(this);">
<span id="phone_ErroM essage"
style="color: red; font-weight: bold; visibility: hidden;"
>Phone number must be digits, spaces and brackets only</span>

A quick example only, not intended for production use. Validate at
the server always.
--
Rob
Aug 5 '08 #3
In comp.lang.javas cript message <619396d5-74a5-4b69-8d81-3e22cf669744@a8
g2000prf.google groups.com>, Tue, 5 Aug 2008 00:05:36, RobG
<rg***@iinet.ne t.auposted:
>
function isPhoneChars(n) {
return /^[0-9 ()]*$/.test(n);
}
That accepts unreasonably short numbers, including "". There is
probably a known minimum length for phone numbers - the Falklands seem
to use 5 digits internally, for example, and Ascension maybe 4. There's
also, IIRC, a maximum allowable length, maybe larger than expected. The
OP should consult ITU recommendations before proceeding.

That allows characters ( ) which cannot be dialled. For consistency, it
should also allow the characters + and - which occur in standard and/or
common forms of printed number.

Since, as well as 0 1 2 3 4 5 6 7 8 9, there are keys for * and #, could
it be that those also ought to be allowed?

<URL:http://www.merlyn.demo n.co.uk/js-valid.htm#VTNre fers. Also
<http://en.wikipedia.or g/wiki/Telephone_numbe rs>.

--
(c) John Stockton, nr London UK. ?@merlyn.demon. co.uk IE7 FF2 Op9 Sf3
news:comp.lang. javascript FAQ <URL:http://www.jibbering.c om/faq/index.html>.
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 5 '08 #4
On Aug 5, 11:51 am, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
wrote:
Abhishek wrote:
Hi this is my another validator in javascript to validate the Phone
Number :-)
[...]
Please feel free to give Comments and Suggessions and Bugs also :-)

Please stop posting your clueless, invalid junk code (here). Thanks in advance.

PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann

Hi Bjoern Hoehrmann, I m sure u haven't tried to run this code, this
code not just for validating the Phone Number, but it has some more .
But anways thanks for ur useful comment regarding my "JUNK POST"..
Keep Posting :)
Aug 7 '08 #5
On Aug 7, 5:07*am, Abhishek <smartwebco...@ gmail.comwrote:
On Aug 5, 11:51 am, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
wrote:
Abhishek wrote:
Hi this is my another validator in javascript to validate the Phone
Number :-)
[...]
Please feel free to give Comments and Suggessions and Bugs also :-)
Please stop posting your clueless, invalid junk code (here). *Thanks in advance.
PointedEars
--
* * realism: * *HTML 4.01 Strict
* * evangelism: XHTML 1.0 Strict
* * madness: * *XHTML 1.1 as application/xhtml+xml
* * * * * * * * * * * * * * * * * ** * * * * * * * -- Bjoern Hoehrmann

Hi Bjoern Hoehrmann, I m sure u haven't tried to run this code, this
Oh brother. I'm sure he hasn't either.
code not just for validating the *Phone Number, but it has some more .
How very.
But anways thanks for ur useful comment regarding my "JUNK POST"..
Keep Posting :)
Nice attitude. And if you could refrain from posting junk here, that
would be good too! ;)
Aug 7 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
21345
by: NotGiven | last post by:
I have a field called telephone whose ONBLUR action is to call a javascript function: validatePhoneNumber(telephone) The non-working function is: function validatePhoneNumber(v) { var phone =document.form. + v + .value; //HERE"S WHAT IS NOT WORKING
7
4892
by: AnnMarie | last post by:
My JavaScript Form Validation doesn't work at all in Netscape, but it works fine in IE. I made some of the suggested changes which enabled it to work in IE. I couldn't make all the changes because then it didn't work in IE. How can I enable this javascipt form validation to work in Netscape? When I use netscape, none of the alert boxes appear. It submits the form without validating anything.
19
6929
by: dmiller23462 | last post by:
Hi guys....I have absolutely NO IDEA what I'm doing with Javascript but my end result is I need two text boxes to stay hidden until a particular option is selected....I've cobbled together the JavaScript in this code from a couple different sites but I'm not 100% sure what each line is doing...This is the ASP code that I'm using for the page....Take a look at the JavaScript code and please let me know what each line is doing....I have been...
7
3614
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title> </head> <style type="text/css">
3
1580
by: pragan | last post by:
HI, I need to create an online form with Details such as Name, Address, EMail, Phone, Message, Credit Card Type/Number/Expiry Date. I started this and I was able to create the form and validate it..but the problem is I have two js files :one to validate the normal fields and other one for credit card(Got through internet).So I decided to have two pages :In first apge user will enter the particulars and once they click the continue...
2
2394
by: Neo Geshel | last post by:
Greetings, I have a form with a telephone field. It is very specific, as it has four text boxes - the country code, area code, prefix and suffix. I can validate each of them individually, but I am stumped as to how to validate them as a group (as one final validation). I need to check to see if all (at one time) are filled or empty. The other individual validations handle cases where the text boxes are filled with letters or ...
1
1840
by: pstephen01010101 | last post by:
I've ran out of debugging ideas. If anyone knows why this won't run in IE 6 please let me know. It works fine in Fire Fox 2.0.0.3 and Netscape (version unknown). I suspect there is something basic about IE and Javascript that I'm wholly unaware of. I've edited the HTML code to include just the essential elements because it exceeded the allowed word count. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
1
5904
by: jhansi | last post by:
hi... I have created a form it's working properly.... but i want to consider phone number and STD code in two different text field for a single lable phone number..... and I have considered the fields like first name,last name,Date of birth, Phone Number ,mobile number and address... 1)If i enter all the fields then only the values should enter into the database... 2) if i dint enter the mobile field then also it should save into...
5
2638
by: ghjk | last post by:
I have a web site with several web pages. They are having different number of variables. eg: first page having 4 variables in the form(Name, Phone, ID, Gender) second page having 10 variables in the form(eg: Name, Age, Time, etc.) I want to validate each and every variable. So how can I do that? I found this JavaScript.But in this case for each and every variable I have to write a function.Eg:for each and every variable i have to rewrite the...
0
8959
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9326
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9248
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9199
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6747
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6049
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4821
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2738
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.