Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Feb 2006
    Posts
    138
    Thanks
    11
    Thanked 0 Times in 0 Posts

    How to test for string length?

    The following function takes three different fields (which combined make up a phone number) and tests it to make sure that all the characters entered are integers. If this is true, it sets form.Phone.value = entirephonenumber. I would like to incorporate an additional test which, after determining that all characters in the string are integers, checks to make sure that the string length is 10. If it is less than 10 or more, I want the entire function to return false and display an error message (the same one is fine..) Any help with getting this accomplished is greatly appreciated. Thanks in advance!

    Code:
    function validateMyForm(form) {
    
    
    
    var entirephonenumber = document.entryform.phoneareacode.value + document.entryform.phoneexchange.value + document.entryform.phonenumber.value
    
    
    
         if (parseInt(entirephonenumber) !=
    
                entirephonenumber) {
    
    		alert('Please enter a valid phone number, numbers only');
    
    		return false;
    
    	} else {
    
        form.Phone.value = entirephonenumber;
    
    	}
    
    	
    
    		return true;
    
    }

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,513
    Thanks
    3
    Thanked 504 Times in 491 Posts
    Code:
    function validateMyForm(form) {
     var entirephonenumber = document.entryform.phoneareacode.value + document.entryform.phoneexchange.value + document.entryform.phonenumber.value
     if (parseInt(entirephonenumber) !=entirephonenumber) {
      alert('Please enter a valid phone number, numbers only');
      return false;
     }
     if (entirephonenumber.length!=10){
      alert('10 Digits REqd');
      return false;
     }
     form.Phone.value = entirephonenumber;
     return true;
    }
    or

    Code:
    function validateMyForm(form) {
     var entirephonenumber = document.entryform.phoneareacode.value + document.entryform.phoneexchange.value + document.entryform.phonenumber.value
     if (isNaN(entirephonenumber)) {
      alert('Please enter a valid phone number, numbers only');
      return false;
     }
     if (entirephonenumber.length!=10){
      alert('10 Digits REqd');
      return false;
     }
     form.Phone.value = entirephonenumber;
     return true;
    }
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    Regular Coder
    Join Date
    Feb 2006
    Posts
    138
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Many thanks Vic!


  •  

    Posting Permissions

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