且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

没有警报框的验证消息

更新时间:2022-10-16 23:12:36

使用 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>

Use innerHTML property to add a text to the span.

document.getElementById("spnFirstName").innerHTML = "some text";



And, input type should be "text" and not "textbox".