Javascript – checking text field value length

javascript

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.

Best Solution

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