sohail
sohail

Reputation: 1

Function working fine in Chrome and IE but not in FireFox

It is the function that i am using to validate my form's name field. This code is working fine in Chrome and IE but not in FireFox.

When check it with firbug it gives this error:

chkForm is not defined

On this line:

if ( chkForm.name.value == "" ).

Thanks in advance

function uname()
{   
    if ( chkForm.name.value == "" )
    {
        alert("Please fill in Username box");
        chkForm.name.focus();
        return false;
    }
    return true;
}

This is the html form

<form name="chkForm" id="chkForm" method="post" action="" onsubmit="return Form_Validator(this)">
<table border="0" cellpadding="0" cellspacing="0" width="550" id="table1">
    <tr>
        <td width="135">&nbsp;</td>
        <td width="138">&nbsp;</td>
        <td width="215">&nbsp;</td>
    </tr>
    <tr>
        <td width="135">Username</td>
        <td width="138">
        <input type="text" name="name" id="username" onblur="return uname()" size="20" class="input_s1_normal"></td>
        <td width="215">
        <div id="nameInfo" align="left"></div>
        </td>
    </tr>
    <tr>
        <td width="135">Email</td>
        <td width="138">
        <input type="text" name="email" id="email" size="20" class="input_s1_normal"></td>
        <td width="215">
        <div id="emailInfo" align="left"></div>
        </td>
    </tr>       
    <tr>
        <td width="135">&nbsp;</td>
        <td width="138">
        <input type="submit" value="SAVE" name="B1" class="button_s1"></td>
        <td width="215">&nbsp;</td>
    </tr>
</table>

Upvotes: 0

Views: 610

Answers (3)

sohail
sohail

Reputation: 1

Thanx to all of you. This code is working now after I add following line in my code

var chkForm = document.getElementById('chkForm');


function uname()
{   
    var chkForm = document.getElementById('chkForm');
    if ( chkForm.name.value == "" )
    {
        alert("Please fill in Username box");
        chkForm.name.focus();
        return false;
    }
     return true;
}

Bundles of Thanks :-) Sohail Ahmad

Upvotes: 0

Ben Taber
Ben Taber

Reputation: 6711

Making an assumption here because can't see all of your code, by I assume you're relying on the fact that IE and Chrome allow access to the dom for elements with IDs through a global var of that ID.

You need to actually define the variable and get a reference to the node like so:

var chkForm = document.getElementById('chkForm');

Upvotes: 1

Talha Ahmed Khan
Talha Ahmed Khan

Reputation: 15433

You can check whether chkForm is defined or not like this.

Its may not be the ideal way to do that but you will get the general idea how to do that.

function uname()
{   
    if(!(chkForm)){
        chkForm = document.getElementByID('chkForm');
    }
    if ( chkForm.name.value == "" )
    {
        alert("Please fill in Username box");
        chkForm.name.focus();
        return false;
    }
    return true;
}

Upvotes: 0

Related Questions