Reputation: 641
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.
Upvotes: 5
Views: 82813
Reputation: 242
you can try this function
var a = txtTelNo1.value;
if (a.length != 10) {
alert('Phone number should be 10 characters');
return false;
}
Upvotes: 0
Reputation: 3540
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>
Upvotes: 1
Reputation: 664395
I've spotted various errors:
document.write()
, it will overwrite the currently open documentform2
is no property of document
. You may use document.forms.form2
, but document.getElementById("t")
is simpler here<button type="submit">
or <input type="submit" />
pattern=".{4}"
for the input or give it a required
attributeUpvotes: 0
Reputation: 2223
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>
Upvotes: 0
Reputation: 9244
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).
Upvotes: 0
Reputation: 26228
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()
.
Upvotes: 0
Reputation: 414
Change your submit button to type="submit". The form is never getting submitted so the validate function isn't being called.
Upvotes: 5
Reputation: 2399
You probably want greater than or equal to (>=), instead of not equal to (!=) if you want "at least a certain length"
Upvotes: 0