Library tutorials & articles
Validate Phone / Fax Number In Javascript
By Pradip Patel, published on 26 Apr 2006
Introduction
There is one javascript function called "ValidateNo".
In this funciton there are two arguments
function ValidateNo(NumStr, String)
{
for(var Idx=0; Idx<NumStr.length; Idx++)
{
var Char = NumStr.charAt(Idx);
var Match = false;
for(var Idx1=0; Idx1<String.length; Idx1++)
{
if(Char == String.charAt (Idx1))
Match = true;
}
if (!Match)
return false;
}
return true;
}
Now create on javascript function called "ValidateDetail".
function ValidateDetail()
{
if(document.myform.phone.value == "")
{
alert("Please specify phone number");
document.myform.phone.focus();
return false;
}
if(!ValidateNo(document.myform.phone.value,"1234567890+- "))
{
alert("Please Enter Only Number");
document.myform.phone.focus();
return false;
}
return true;
}
Now you can call this javascript "ValidateDetail" function for validating your phone and fax number.
In this funciton there are two arguments
- NumStr : This is the value which you want to validate. It will come from your form control. It may by TextBox control (HTML or ServerControl).
- String : This is predefined format which you can use to validate phone/fax number. It may contain +, - and space. You can modify it as per your requirement.
function ValidateNo(NumStr, String)
{
for(var Idx=0; Idx<NumStr.length; Idx++)
{
var Char = NumStr.charAt(Idx);
var Match = false;
for(var Idx1=0; Idx1<String.length; Idx1++)
{
if(Char == String.charAt (Idx1))
Match = true;
}
if (!Match)
return false;
}
return true;
}
Now create on javascript function called "ValidateDetail".
function ValidateDetail()
{
if(document.myform.phone.value == "")
{
alert("Please specify phone number");
document.myform.phone.focus();
return false;
}
if(!ValidateNo(document.myform.phone.value,"1234567890+- "))
{
alert("Please Enter Only Number");
document.myform.phone.focus();
return false;
}
return true;
}
Now you can call this javascript "ValidateDetail" function for validating your phone and fax number.
Related articles
Related discussion
-
Auto fill up webpage using java script
by irfanmaitla (0 replies)
-
Free download javascript ebooks
by jmaneesh (0 replies)
-
Adding date in javascript function
by nattyp (2 replies)
-
Search text in a Word document with javascript
by ill_comms (0 replies)
-
Which is harder to learn Java or C++ ?
by SanjayMKatta (1 replies)
Related podcasts
-
Java Posse #220 - JavaFX Launch and Interview
JavaFX Launch and Interview Fully formatted shownotes can always be found at http://javaposse.com An Interview with John Burkey and Octavian Tanase of Sun Microsystems about JavaFX. The audio quality is telephone grade, you have been warned. JavaFX http://www.sun.com/software/javafx/ Octav...
Events coming up
-
Dec
6
Developing AJAX Web Applications with Castle Monorail
London, United Kingdom
Monorail is the model-view-controller engine of the Castle Project, bringing many of the best ideas of Ruby on Rails to the .NET world. In this talk, David De Florinier and Gojko Adzic show how Monorail makes it easy to develop .NET based AJAX applications, and how to use the Castle Project to build Web 2.0 applications effectively. Come to this session if you are a .NET web developer. Everyone is welcome!
This thread is for discussions of Validate Phone / Fax Number In Javascript.