By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,462 Members | 3,388 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,462 IT Pros & Developers. It's quick & easy.

number validation and string validation

P: 1
how to validate phone number,age using javascript with html.reply with simple source code .i am well in javascript. give explanation with simple example
Sep 1 '07 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 173
Hi Haji,

Welcome to TSDN...

How accurate are you wanting the validation to be and what are the conditions?

I would assume that the age one is quite simple for example it must be a number, greater than or equal to 0 (incase you have some very young users :)) and less than 120 (unless you have very old users).

The phone number is more involved as it depends on the country that the telephone number is in due to differences in format. If the condition is as simple as it needs to be a number and in a certain length range then this is simple to code, more complex matching can be done using regular expressions to make sure that the submitted phone number matches a pattern.

Sample code for the age (based on the conditions above would be):
Expand|Select|Wrap|Line Numbers
  1. function checkAge($age){
  2.   // Create a variable to return, set initial value to false,
  3.   // this will be changed if the checks are passed.
  4.  
  5.   var $blAge = false;
  6.  
  7.   //Check if the $age is a number and not an empty string.
  8.   if (!isNaN($age) && $age.length>0){
  9.  
  10.     // Make sure $age within the defined range.
  11.     if($age>=0 && $age<120){
  12.       // $age is a valid age so change the return value to true.
  13.       $blAge=true
  14.     }
  15.   }
  16.  
  17.   //return the return value
  18.   return $blAge;
  19. }
  20.  
The code above creates a function that checks the value passed in and returns true if it is a valid age or false otherwise. The two if conditions could be combined into one, I have put them seperately for clarity.

Please let me know your criteria for the phone number and I can go through that with you.
Sep 1 '07 #2

pbmods
Expert 5K+
P: 5,821
Heya, Haji. Welcome to TSDN!

TSDN members are expected to write their own code. We're happy to help you out if you have specific questions, but you can't realistically expect someone to do your coding for you on a free forum.
Sep 2 '07 #3

acoder
Expert Mod 15k+
P: 16,027
how to validate phone number,age using javascript with html.reply with simple source code .i am well in javascript. give explanation with simple example
For the phone number, use regular expressions.
Sep 2 '07 #4

Post your reply

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