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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Posts
    418
    Thanks
    18
    Thanked 2 Times in 2 Posts

    form input length validation

    I'm trying to figure out how to add a length paramater to this function. Not quite sure how to do it. here's what I tried:

    Code:
    var phone = document.getElementById('phone');
    
    if(isNumericdashes(phone, "Please enter a valid phone number")){
    									return true;
    									}
    
    function isNumericdashes(elem, helperMsg){
    	var numericExpression = /^[0-9\(\)-]+$/;
    	if(elem.value.match(numericExpression) &&(elem.length > 9)){
    		return true;
    	}else{
    		alert(helperMsg);
    		elem.focus();
    		return false;
    	}
    }

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    if ( (elem.value.match(numericExpression) ) && (elem.length > 9) ){

    Missing brackets.
    Last edited by Philip M; 03-11-2012 at 10:50 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by turpentyne View Post
    Code:
    var phone = document.getElementById('phone');
    
    if(isNumericdashes(phone, "Please enter a valid phone number")){
    									return true;
    									}
    Presumably the above is code from another function, but you don't seem to have provided a way to return false..

    The test you need is:
    Code:
    if( elem.value.match( numericExpression ) &&  elem.value.length > 9 )
    Also you may need to escape the '-' : /^[0-9\(\)\-]+$/

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Logic Ali View Post
    PAlso you may need to escape the '-' : /^[0-9\(\)\-]+$/
    - doesn't need to be escaped if it is the last character inside the []
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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