0

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 == '&nbsp;') { this.value = '&nbsp;Name'; }"
                            onfocus="if(this.value == '&nbsp;Name') { this.value = '&nbsp;'; }" value="&nbsp;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 == '&nbsp;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;
    }}

enter image description hereenter image description here

It is not showing alerting if I don't enter anything in the Name field. The Hotel validation works fine.

1
  • 1
    You need to put the internal encoding of the &nbsp; entity in the string. I'm not sure how to do that. Commented Mar 7, 2013 at 7:01

2 Answers 2

1

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?

Sign up to request clarification or add additional context in comments.

Comments

0

Change this line:

if (document.getElementById('<%= LName.ClientID %>').value == '&nbsp;Name') {

to:

if (document.getElementById('<%= LName.ClientID %>').value == String.fromCharCode(160) + 'Name') {

1 Comment

try again. &nbsp; has a char code of 160, not 32 as I thought.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.