SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Feb 2000
    Location
    Minnesota
    Posts
    317
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using Javascript for form validation: How can I check that the phone # field is all

    numbers but allow "-" or "/" or "(" characters?
    Code:
    function validateNumber(field, msg, min, max) {
    	if (!min) { min = 0 }
    	if (!max) { max = 255 }
    
    	if ( (parseInt(field.value) != field.value) ||
                 field.value.length < min ||
                 field.value.length > max) {
    		alert(msg);
    		field.focus();
    		field.select();
    		return false;
    	}
    
    	return true;
    }
    Thanks!
    Keomed.com Webmaster
    www.keomed.com

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,236
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Read up on regular expresssions. It should help you out with your problem. I would post a sample or fix, but I don't have enough time. I'll try to post when I get home from work.

    --Vinnie

  3. #3
    SitePoint Evangelist
    Join Date
    Nov 2002
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check the example below:

    http://asp-hosting.ca/validatephone.htm

    You'll have to modify it a bit to allow "(" and "/".
    If you are new to the regular expressions then read this first:

    http://www.evolt.org/article/rating/20/22700/

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2002
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This function will return true if the string is a US phone number formatted as one of the following...
    (000)000-0000, 000-000-0000, 000.000.0000, 0000000000

    Code:
    function isPhoneNumber(str){
    	var pattern = /^\(?\d{3}[\)-\.]?\d{3}[-\.]?\d{4}$/
    	return pattern.test(str);
    }
    travis

  5. #5
    SitePoint Addict
    Join Date
    Feb 2000
    Location
    Minnesota
    Posts
    317
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for all your help! I tweaked it a bit so it would accept spaces as well: 000 000 0000
    Here's the code with RegExp:
    Code:
    /* The regular expression looks for inorder:
    zero or one open parenthesis \(?
    followed by three digits \d{3} 
    followed by zero or one close parenthesis \)? 
    followed by zero or one dash, forward slash, decimal point or space
    followed by three digits \d{3} 
    followed by zero or one dash, forward slash, decimal point or space
    followed by four digits \d{4}. */
    function validatePhoneNumber(field, msg, min, max) {
    	if (!min) { min = 0 }
    	if (!max) { max = 255 }
    	
    	var re_phone = /\(?\d{3}\)?[-\/\.\s]?\d{3}[-\/\.\s]?\d{4}/;
    	if (!field.value || 
    			!re_phone.test(field.value) ||
    			field.value.length < min || 
    			field.value.length > max) {
    				alert(msg);
    				field.focus();
    				field.select();
    				return false;
    	}
    
    	return true;
    }
    Used with:
    Code:
    <form name="whatever" method="post" onsubmit="return (
    validatePhoneNumber(this.input_name,
          'Please enter your phone number.', 10, 14) && 
       );">
    Keomed.com Webmaster
    www.keomed.com


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •