如何在Javascript中使文本框成为必填字段 [英] How Do I Make Textbox A Required Field In Javascript
本文介绍了如何在Javascript中使文本框成为必填字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想让我的文本框必填字段ID是BotBootInput我应该把什么放在document.write行
I want make my textbox REQUIRED FIELD id is BotBootInput what should i put in "document.write" line
<script type="text/javascript">
var a = Math.ceil(Math.random() * 10);
var b = Math.ceil(Math.random() * 10);
var c = a + b
function DrawBotBoot()
{
document.write("What is "+ a + " + " + b +"? ");
document.write("<input id='BotBootInput' type='text' maxlength='2' size='2'/>");
}
function ValidBotBoot(){
var d = document.getElementById('BotBootInput').value;
if (d == c) return true;
return false;
}
</script>
请帮助我。
PLEASE HELP ME.
推荐答案
你可以使用这个:http://www.w3schools.com/tags/att_input_required.asp [ ^ ] ...
You may use this: http://www.w3schools.com/tags/att_input_required.asp[^]...
您可以将HTML代码用作:
You can use the HTML Code as :
<form action="">
Username: <input type="text" name="usrname" required="">
<input type="submit">
</input></input></form>
您可以使用HTML& JavaScript:
You can use the HTML & JavaScript:
<form name="myForm" action="" önsubmit="return validateForm()" method="post">
First name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
<script language="JavaScript">
function validateForm() {
var x = document.forms["myForm"]["fname"].value;
if (x == null || x == "") {
alert("First name must be filled out");
return false;
}
}
</script>
http://www.netmechanic.com/news/vol6/javascript_no4.htm [ ^ ]
http://msdn.microsoft.com/en-us/library/office/aa218652(v=office.11).aspx [ ^ ]
http://www.w3schools.com/JS/js_form_validation.asp [ ^ ]
http://www.netmechanic.com/news/vol6/javascript_no4.htm[^]
http://msdn.microsoft.com/en-us/library/office/aa218652(v=office.11).aspx[^]
http://www.w3schools.com/JS/js_form_validation.asp[^]
这篇关于如何在Javascript中使文本框成为必填字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文