...

View Full Version : require min characters in text box



awayne96
12-07-2009, 01:47 AM
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!


<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>

Philip M
12-07-2009, 08:38 AM
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

awayne96
12-07-2009, 08:33 PM
Thanks, that worked!

I greatly appreciate it.

Old Pedant
12-07-2009, 09:09 PM
Fair warning: With that code the user could enter 3 spaces and you would accept that!

I might at least change it to:

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.

Philip M
12-07-2009, 11:23 PM
Fair warning: With that code the user could enter 3 spaces and you would accept that!

I might at least change it to:

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.:-


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. :eek:

Old Pedant
12-08-2009, 01:03 AM
Of course, the user can still enter xxx.

Yep. And the odds are 79.3 to 1 that some user will.

awayne96
12-08-2009, 06:26 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum