没有警报框的验证消息 [英] Validation message without alert box
问题描述
我希望验证消息在单击提交按钮时以红色显示在文本框下。我添加了span,但它不起作用....
给出代码下面:
函数验证()
{
var valfirst = document.getElementById( FirstName)。value;
var valsecond = document.getElementById( SecondName)。value;
if (valfirst == null || valfirst == )
{
document.getElementById( spnFirstName)。value =
名字是必需的 ;
return false;
}
else if (valsecond == null || valsecond == < span class =code-string> )
{
document.write( 第二个名称是必需的);
return false;
}
}
< pre lang =xml> < html >
< body > ;
< script type = text / javascript src = External.js >
< / script >
< 表格 < span class =code-attribute> id = frm1 方法 = 获取 >
名字:< 输入 类型 = textbox id = FirstName / >
< span id = spnFirstName > < / span > ;
其他名称:< 输入 type = textbox id = < span class =code-keyword> SecondName / >
< 输入 类型 = 按钮 value = 提交 onClick = 返回验证() / >
< 输入 类型 = 按钮 value = 清除 onClick = reset() / >
< / form >
< / body >
< / html >
使用innerHTML
属性为跨度添加文本。
document.getElementById( spnFirstName)。innerHTML = some text;
And ,输入类型应为text而不是textbox。
I want the validation message to be displayed under the text box in red color when the submit button is clicked.I added span, but it is not working....
The code is given below:
function validation()
{
var valfirst= document.getElementById("FirstName").value;
var valsecond=document.getElementById("SecondName").value;
if(valfirst == null || valfirst == "")
{
document.getElementById("spnFirstName").value=
"First Name is required";
return false;
}
else if (valsecond == null || valsecond == "")
{
document.write("Second Name is required");
return false;
}
}
<html>
<body>
<script type="text/javascript" src="External.js">
</script>
<form id="frm1" method="get">
First Name: <input type="textbox" id="FirstName"/>
<span id="spnFirstName" ></span>
Second Name:<input type="textbox" id="SecondName"/>
<input type="button" value="Submit" onClick="return validation()"/>
<input type="button" value="Clear" onClick="reset()"/>
</form>
</body>
</html>
UseinnerHTML
property to add a text to the span.
document.getElementById("spnFirstName").innerHTML = "some text";
And, input type should be "text" and not "textbox".
这篇关于没有警报框的验证消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!