简体   繁体   中英

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;
    }}

在此处输入图片说明在此处输入图片说明

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

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?

Change this line:

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

to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM