Jason28 Posted September 17, 2008 Share Posted September 17, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/124608-solved-javascript-string-length-validation/ Share on other sites More sharing options...
RichardRotterdam Posted September 17, 2008 Share Posted September 17, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/124608-solved-javascript-string-length-validation/#findComment-643591 Share on other sites More sharing options...
Jason28 Posted September 17, 2008 Author Share Posted September 17, 2008 Yes it is a textfield and your first code worked thanks a lot Quote Link to comment https://forums.phpfreaks.com/topic/124608-solved-javascript-string-length-validation/#findComment-643593 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.