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 7 of 7
  1. #1
    New Coder
    Join Date
    Oct 2009
    Posts
    70
    Thanks
    6
    Thanked 0 Times in 0 Posts

    require min characters in text box

    I am using the below javascript to verify a text input box but I would also like to require atleast 3 characters being entered before allowing the user to search. How do I accomplish that? Thanks in advance!

    Code:
    <script language="JavaScript">
    <!--
    
    /***********************************************
    * Required field(s) validation v1.10- By NavSurf
    * Visit Nav Surf at http://navsurf.com
    * Visit http://www.dynamicdrive.com/ for full source code
    ***********************************************/
    
    function formCheck(formobj){
    	// Enter name of mandatory fields
    	var fieldRequired = Array("words");
    	// Enter field description to appear in the dialog box
    	var fieldDescription = Array("Company Name");
    	// dialog message
    	var alertMsg = "Please complete the following fields:\n";
    	
    	var l_Msg = alertMsg.length;
    	
    	for (var i = 0; i < fieldRequired.length; i++){
    		var obj = formobj.elements[fieldRequired[i]];
    		if (obj){
    			switch(obj.type){
    			case "select-one":
    				if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
    					alertMsg += " - " + fieldDescription[i] + "\n";
    				}
    				break;
    			case "select-multiple":
    				if (obj.selectedIndex == -1){
    					alertMsg += " - " + fieldDescription[i] + "\n";
    				}
    				break;
    			case "text":
    			case "textarea":
    				if (obj.value == "" || obj.value == null){
    					alertMsg += " - " + fieldDescription[i] + "\n";
    				}
    				break;
    			default:
    			}
    			if (obj.type == undefined){
    				var blnchecked = false;
    				for (var j = 0; j < obj.length; j++){
    					if (obj[j].checked){
    						blnchecked = true;
    					}
    				}
    				if (!blnchecked){
    					alertMsg += " - " + fieldDescription[i] + "\n";
    				}
    			}
    		}
    	}
    
    	if (alertMsg.length == l_Msg){
    		return true;
    	}else{
    		alert(alertMsg);
    		return false;
    	}
    }
    // -->
    </script>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Code:
    case "text":
    if (obj.value.length < 3 || obj.value == null){
    alertMsg += " - (you must enter 3 characters minimum)  " + fieldDescription[i] + "\n";
    }
    break;
    <script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead.
    The <!-- and //--> comment (hiding) tags have not been necessary since IE3. If you see these in some published script it is a warning that you are looking at ancient and perhaps unreliable code.



    "A man would do nothing, if he waited until he could do it so well that no one at all would find fault with what he has done." - Cardinal Newman

  • Users who have thanked Philip M for this post:

    awayne96 (12-07-2009)

  • #3
    New Coder
    Join Date
    Oct 2009
    Posts
    70
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thanks, that worked!

    I greatly appreciate it.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,889
    Thanks
    79
    Thanked 4,421 Times in 4,386 Posts
    Fair warning: With that code the user could enter 3 spaces and you would accept that!

    I might at least change it to:
    Code:
    case "text":
        if (obj.value == null || obj.value.replace(/\s/g,"").length < 3 ){
           alertMsg += " - (you must enter 3 characters minimum)  " + fieldDescription[i] + "\n";
        } 
        break;
    I don't think it's possible to have a null for obj.value if indeed obj is a text field, but if you are going to test for null, you should do that *before* you then try to use the value! If it is null, then obj.value.replace() would cause an error.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    awayne96 (12-08-2009)

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by Old Pedant View Post
    Fair warning: With that code the user could enter 3 spaces and you would accept that!

    I might at least change it to:
    Code:
    case "text":
        if (obj.value == null || obj.value.replace(/\s/g,"").length < 3 ){
           alertMsg += " - (you must enter 3 characters minimum)  " + fieldDescription[i] + "\n";
        } 
        break;
    I don't think it's possible to have a null for obj.value if indeed obj is a text field, but if you are going to test for null, you should do that *before* you then try to use the value! If it is null, then obj.value.replace() would cause an error.
    Yes, but the user could enter *?! as well which is not much better than three spaces. Depending on the actual field, it would be best of all to use a suitable regex, e.g.:-

    Code:
    obj.value = obj.value.replace(/^\s+|\s+$/g,"");  // strip leading and trailing spaces
    obj.value = obj.value.replace(/[^a-z0-9\s]/gi,"");  remove non-alphanumerics - retain intermediate spaces
    if (obj.value.replace(/\s/g,"").length < 3 ) {  // length of remaining string ignoring spaces
    alertMsg += " - (you must enter 3 characters minimum)  " + fieldDescription[i] + "\n";
    }
    Of course, the user can still enter xxx.
    Last edited by Philip M; 12-07-2009 at 10:42 PM.

  • Users who have thanked Philip M for this post:

    awayne96 (12-08-2009)

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,889
    Thanks
    79
    Thanked 4,421 Times in 4,386 Posts
    Of course, the user can still enter xxx.
    Yep. And the odds are 79.3 to 1 that some user will.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New Coder
    Join Date
    Oct 2009
    Posts
    70
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thanks to both of you for the help.

    I have updated it with the suggestions you added.

    The nice thing is the user can put 3 spaces and get nothing in the results.


  •  

    Posting Permissions

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