prema
prema

Reputation: 427

Javascript alert not getting displayed

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.

Upvotes: 0

Views: 248

Answers (2)

Nikolaj Zander
Nikolaj Zander

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

Ray Cheng
Ray Cheng

Reputation: 12566

Change this line:

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

to:

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

Upvotes: 0

Related Questions