Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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:
Java
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;
  } 
}




XML
<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>
Posted
Updated 5-Sep-18 1:03am
v3
Comments
justinonday 13-Jan-11 4:52am    
Always put code on Code Block...
JF2015 13-Jan-11 4:54am    
Added pre tags to improve code formatting.
Ankur\m/ 13-Jan-11 6:13am    
When innerHTML is added to display the text it is working.... Thanks mate.......
You should then accept the answer and vote it up.
Cheers!
Ankur\m/ 13-Jan-11 6:30am    
It seems you voted it a 2 which is not a good vote.
1 is the lowest, 5 - the highest.
Ankur\m/ 13-Jan-11 6:34am    
Seems good now. Thanks! :)

1 solution

Use innerHTML property to add a text to the span.

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


And, input type should be "text" and not "textbox".
 
Share this answer
 
Comments
Lyn123 13-Jan-11 5:23am    
Since value is entered in a text box, Input type is Text box not text
Ankur\m/ 13-Jan-11 5:36am    
input is an HTML element. input type text is used for rendering a textbox on the browser.
Or use, ASP.NET control: <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
Lyn123 13-Jan-11 6:10am    
When innerHTML is added to display the text it is working.... Thanks mate.......
Espen Harlinn 13-Jan-11 7:27am    
5+ for being right - again :)
Ankur\m/ 13-Jan-11 7:31am    
Thanks mate. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900