最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - checking text field value length - Stack Overflow

programmeradmin3浏览0评论

I am trying to see if the text field length is at least a certain length. here is my code:

<form name="form2" id="form2" onsubmit="return validate()">
length 4: <input type="text" name = "t" id="t" />
<input type="button" name="submit" value="submit" />
</form>

<script>
function validate() {
    document.write("good");
    submitFlag = true;
    if(document.form2.t.value.length!=4){
        submitFlag=false;
        alert("ivalid length - 4 characters needed!");
    }
    return submitFlag;
}
</script>

when I click submit, nothing happens.

I am trying to see if the text field length is at least a certain length. here is my code:

<form name="form2" id="form2" onsubmit="return validate()">
length 4: <input type="text" name = "t" id="t" />
<input type="button" name="submit" value="submit" />
</form>

<script>
function validate() {
    document.write("good");
    submitFlag = true;
    if(document.form2.t.value.length!=4){
        submitFlag=false;
        alert("ivalid length - 4 characters needed!");
    }
    return submitFlag;
}
</script>

when I click submit, nothing happens.

Share Improve this question asked Apr 18, 2012 at 19:51 droidusdroidus 6415 gold badges14 silver badges25 bronze badges 1
  • 1 You're using document.write() in wrong context again... – Teemu Commented Apr 18, 2012 at 19:57
Add a comment  | 

8 Answers 8

Reset to default 5

Change your submit button to type="submit". The form is never getting submitted so the validate function isn't being called.

The input type needs to be "submit"

<form name="form2" id="form2" onsubmit="return validate()">
   length 4: <input type="text" name = "t" id="t" />
    <input type="submit" name="submit" value="submit" />  <!--INPUT TYPE MUST BE SUBMIT -->
    </form>

    <script>
    function validate() {
        document.write("good");
        submitFlag = true;
        if(document.form2.t.value.length!=4){
            submitFlag=false;
            alert("ivalid length - 4 characters needed!");
        }
        return submitFlag;
    }
</script>

You probably want greater than or equal to (>=), instead of not equal to (!=) if you want "at least a certain length"

Your button should be

<input type="submit" name="submit" value="submit" />

otherwise the onsubmit event will not occur. Also, use console.log() for debugging, rather than document.write().

  • DEMO

There's a few issues that you have. 1) You need to make your input a submit button and 2) you need to remove your document.write() statement. Change your code to the following (or see this jsFiddle):

<form name="form2" id="form2" onsubmit="return validate()">
length 4: <input type="text" name = "t" id="t" />
<input type="submit" name="submit" value="submit" />
</form>

<script>
function validate() {
    submitFlag = true;
    if(document.form2.t.value.length!=4){
        submitFlag=false;
        alert("ivalid length - 4 characters needed!");
    }
    return submitFlag;
}
</script>​​​​

You were pretty close, but you could use some clean-up on that code (Note: I did not provide any).

You should also add a maxlength attribute to your text input, it'll help the user.

<form name="form2" id="form2" onsubmit="return validate();">
    <input type="text" name="t" id="t" maxlength="4"/>
    <input type="submit" name="submit" value="submit"/>
</form>

<script type="text/javascript">
    function validate()
    {
        if ( document.getElementById("t").value.length != 4 )
        {
            alert( "Invalid length, must be 4 characters" );
            return false;
        }   
        return true;
    }
</script>

I've spotted various errors:

  • don't use document.write(), it will overwrite the currently open document
  • form2 is no property of document. You may use document.forms.form2, but document.getElementById("t") is simpler here
  • Your expression doesn't check for "at least 4", it checks for "exactly 4".
  • Your button won't submit anything. Change it to <button type="submit"> or <input type="submit" />
  • you could specify a pattern=".{4}" for the input or give it a required attribute

you can try this function

var a = txtTelNo1.value;
            if (a.length != 10) {
                alert('Phone number should be 10 characters');
                return false;
            }
发布评论

评论列表(0)

  1. 暂无评论