5

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.

1
  • 1
    You're using document.write() in wrong context again... Commented Apr 18, 2012 at 19:57

8 Answers 8

5

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

Sign up to request clarification or add additional context in comments.

Comments

1

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> 

Comments

0

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

Comments

0

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

Comments

0

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

Comments

0

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> 

Comments

0

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

2 Comments

1. what do i use in place? just write the message to a div? 2. form2 is the name/id for the form. i guess this tutorial book that i'm using must be really old.... 3. is there any advantage to using javascript for min/max length?
Yes, after the document is loaded you need to manipulate the DOM; for example manipulate the innerText of a div. @HTML5 validation: The advantage is you won't need javascript (in modern browsers). BTW: min/max are only for numbers
0

you can try this function

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.