mickeymouse Posted June 23, 2019 Share Posted June 23, 2019 Isn't this code correct? I'm trying to test for the length of an input field when submit is clicked. function formcheck() {if(stdcd.value.length < 9){alert(\"MUST BE 9 CHARACTERS.\");return false;} return true;} Thanks Link to comment Share on other sites More sharing options...
Ingolme Posted June 23, 2019 Share Posted June 23, 2019 The variable "stdcd" is not defined, you need to assign an input element to it before you can use it. You can access an element using one of many DOM methods such as getElementById() or querySelector() 1 Link to comment Share on other sites More sharing options...
Makwana Prahlad Posted September 28, 2019 Share Posted September 28, 2019 Hello, @mickeymouse Try this code : <html> <head> <script> function countlength() { var textval = document.getElementById("val"); if (textval.value.length < 9) { alert("MUST BE 9 CHARACTERS." + "\n\nYour input characters length is :- " + textval.value.length); return false; } return true; } </script> <title>Input value length checked</title> </head> <body> <h2>Input value length checked</h2> <input type="text" name="val" id="val" /> <br /><br /> <input type="submit" value="Submit" name="Submit" onclick="countlength()" /> </body> </html> I hope above code will be useful for you. Thank you. Link to comment Share on other sites More sharing options...
AjayGohil Posted October 23, 2019 Share Posted October 23, 2019 (edited) Try this way <input type="text" id="txtName" name="usrname" > <input type="submit" value="Submit" name="Submit" onclick="chklength()" /> <script> function chklength() { var name=$("#txtName").val(); if(name.length < 9){ alert("Name must be nine characters "); } </script> Edited October 23, 2019 by AjayGohil Link to comment Share on other sites More sharing options...
dsonesuk Posted October 23, 2019 Share Posted October 23, 2019 That limits the string length to a max of 9, the js code allows 9 and above. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now