我有一個需要數字的表單,並且有一個特定的(電話號碼,或phNo
)表單元素,我只想接受7位數的長度(不多也不少)。使用JavaScript的理念是:引用HTML數字表單元素的長度?
如果元素長度不等於7:true
其他false
這裏是我的代碼:
var phNo = document.getElementsByName('phNo'); //here I try to get
the form value in the form of an object. This is where I think I am going wrong
var phNoString = phNo.toString(); //Not to sure if I need this line or not, read somewhere that .length only works on a string
if(phNoString.length != 7) {
//Do something for false
return;
} else {
//Do something for true
}
<form id="myForm" action="form_action.asp">
First name: <input type="text" name="fName"><br>
Last name: <input type="text" name="lName"><br>
Phone Number: <input type="number" name="phNo" max="7"><br>
Credit Card Number: <input type="number" name="cardNo"><br>
</form>
<input id="submit" type="button"value="Submit"onClick="detailsSubmit()">
你想'.value',沒有'的ToString()',就像其他任何輸入。 – Bergi
'submit'輸入應該在你的'form'裏面。你不能指定你遇到什麼問題.. – choz