Reputation: 427
I have a form with a text box (Name) and a dropdown (Hotel).
<input type="text" name="lname" runat="server" id="LName" tabindex="1" onblur="if(this.value == ' ') { this.value = ' Name'; }"
onfocus="if(this.value == ' Name') { this.value = ' '; }" value=" Name"
style="color: #000000; border: 1px solid #757575; width: 234px; margin-top: 5px;" />
<div class="dropdownselect" style="margin: 15px 0;">
<asp:DropDownList ID="drhotel" runat="server" Style="border: 1px solid #757575; width: 234px;
height: 16px; *height: 18px;">
</asp:DropDownList>
If nothing is entered the text box will display "Name" by default. I am trying to validate on client click using the method below:
function validatecust() {
if (document.getElementById('<%= LName.ClientID %>').value == ' Name') {
alert('Please enter Name');
document.getElementById('<%= LName.ClientID %>').focus();
return false;
}
if (document.getElementById('<%= drhotel.ClientID %>').value == 'Select Hotel') {
alert('Please select Hotel');
document.getElementById('<%= drhotel.ClientID %>').focus();
return false;
}}
It is not showing alerting if I don't enter anything in the Name field. The Hotel validation works fine.
Upvotes: 0
Views: 248
Reputation: 1270
try:
if (document.getElementById('<%= LName.ClientID %>').value == 'Name') {
Leave the whitespace! If you want to indent the text in the textbox use a css padding!
If the above thing doesnt work, i think you should use a <asp:TextBox/>
instead of a input runat server!
Have you checked what the resulting value of LName.ClientID
and the real Id on your input is?
Upvotes: 1
Reputation: 12566
Change this line:
if (document.getElementById('<%= LName.ClientID %>').value == ' Name') {
to:
if (document.getElementById('<%= LName.ClientID %>').value == String.fromCharCode(160) + 'Name') {
Upvotes: 0