472,958 Members | 2,348 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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

Hi this is my another validator in javascript to validate the Phone
Number :-)
<script language='javascript'>
function funcCheckPhoneNumber(ctrtxtMobile,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(((strCharCode >= 48) && (strCharCode <= 57 )) || ((strCharCode
>= 40) && (strCharCode <= 41 )) || (strCharCode == 45) ||
(strCharCode==32)|| (strkeyCode==37 )|| (strkeyCode==38) ||
(strkeyCode == 46)||(strCharCode==46)||(strkeyCode==8 ) || (strkeyCode
==9 ) ||(strkeyCode==39) || (strkeyCode ==35) || (strkeyCode ==36) ||
(strkeyCode==9)){}
else{
return false;}}
return true;}

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

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

"Onkeypress", "javascript:return funcCheckPhoneNumber(this,event);"
Please feel free to give Comments and Suggessions and Bugs also :-)
Aug 5 '08 #1
5 3619
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.getElementById('phone_ErroMessage');
if (!isPhoneChars(el.value)) {
errEl.style.visibility = 'visible';
} else {
errEl.style.visibility = 'hidden';
}
}
}

</script>
<input type="text" id="phone" onkeyup="validate(this);">
<span id="phone_ErroMessage"
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.javascript message <619396d5-74a5-4b69-8d81-3e22cf669744@a8
g2000prf.googlegroups.com>, Tue, 5 Aug 2008 00:05:36, RobG
<rg***@iinet.net.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.demon.co.uk/js-valid.htm#VTNrefers. Also
<http://en.wikipedia.org/wiki/Telephone_numbers>.

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE7 FF2 Op9 Sf3
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 5 '08 #4
On Aug 5, 11:51 am, Thomas 'PointedEars' Lahn <PointedE...@web.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...@web.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
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...
7
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...
19
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...
7
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>...
3
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...
2
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...
1
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...
1
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...
5
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...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.