Jump to content
mickeymouse

Test <input> field length

Recommended Posts

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 

Share this post


Link to post
Share on other sites

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

  • Like 1

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...