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 |8 Answers
Reset to default 5Change 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 ofdocument
. You may usedocument.forms.form2
, butdocument.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 arequired
attribute
you can try this function
var a = txtTelNo1.value;
if (a.length != 10) {
alert('Phone number should be 10 characters');
return false;
}
document.write()
in wrong context again... – Teemu Commented Apr 18, 2012 at 19:57