Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am writing code for userId availability check, now I am facing problem in submitting form, my javascript code is,

JavaScript
function checkTeacherId(){
alert(" in checkTecherId()");
var status=document.getElementById("status");
if(status=="Id in use choose another")
	return false;
}


html tag is as follow

HTML
<input type="text" class="form-control" name="teacherId"			id="teacherId" placeholder="Enter Teacher Id"  ><span id="status" class="status"></span>

<input type="submit" class="btn btn-primary" value="Submit"					 önclick="checkTeacherId();" />


The value of "span" tag will come from servlet as,
Java
if(contains){
			out2.println("<font color=red id=status>");
			out2.println("Id in use choose another");
			out2.println("</font>");
		}else{
			out2.println("<font color=green id=status>");
			out2.println("Id Available");
			out2.println("</font>");
		}



what I want to do here I dont want to submit the form if I get the value as "Id in use choose another" . I am unable to do it please help me.
Posted
Updated 30-Apr-14 20:59pm
v2

Here span and font (coming from servlet) has the same id. So, you need to change font tag ids. Then you can use following modified version of your checkTeacherId() function:

C#
function checkTeacherId() {
            var status = $("#status").text();
            if (status == "Id in use choose another")
                return false;
            else
                return true;
        }


Here I have used jQuery. $("#status").text() ignores all the html tags inside the span element and returns only the text. That means font tag will be ignored from span element and the text "Id in use choose another" or "Id Available" will be returned.
 
Share this answer
 
Comments
Raghavendra M 1-May-14 6:23am    
No it s not working ., the form is still submitting .
altaf008bd 1-May-14 7:04am    
Hi, did you change font tag IDs? font tag id should be different than your span id. But you used same id "status" in both cases
please post the html content of the span. It works in my test code, the span content I used is:
<span id="status" class="status"><font color="red">Id in use choose another</font></span>
 
Share this answer
 
Comments
Raghavendra M 3-May-14 5:25am    
Yes . , I remove the id of font and now I am getting the span tag exactly as urs . .
Raghavendra M 3-May-14 6:59am    
Hi, I checked with alert , I am getting status value as font color="red"Id in use choose another /font
Raghavendra M 3-May-14 7:00am    
I mean along with font tag
altaf008bd 6-May-14 1:04am    
Sorry, for late reply. I am not sure why you are getting the value with font tag, because text() method simply ignores the html tag and return the text. However you can use :

var status = $("#status font").html();

It will only return the text which you can compare.

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