Jump to content

[SOLVED] Javascript string length validation?


Jason28

Recommended Posts

Hello, I wanted to use an if statement within a working function that alerts the user if the amount of characters that they have entered in the field is too short.  I made up this thinking it would work but it doesn't:

 

if (document.getElementById('searchField').str.length < 3 )
{
	alert("The search keyword is too short please be more specific.");
	document.getElementById('searchField').focus();return(false)
}

 

could anyone fix this for me so if a user enters less than three characters in the field it errors?  Thanks.

is the search field an input/textarea or is it another element.

if the element is an input or textarea you can access the string length like this

 

document.getElementById('searchField').value.length 

otherwise you can access it with innerHTML

document.getElementById('searchField').innerHTML.length 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.