Noora
Noora

Reputation: 319

alert function in JavaScript is not working

I have an old page created in clasic asp, contain a form, I am trying to submit the form using JavaScript function after checking the values available in the form.

HTML:

<form method="POST" action="vendorCheckDates.asp" name="Searchform" id="submit_Form">
    Select Type of Search: </b>
    <select size="1" name="Query" class="button">
        <option selected name="vNAME" value="vNAME"> Name</option>
        <option name="vNUM" value="vNUM"> Number</option>
        <option name="vEM" value="vEM">Email </option>
        <option name="vFAX" value="vFAX">Fax </option>
    </select>
    <b>:</b>
    <input type="text" name="hsearch" id="hsearch">
    <fieldset class="fieldset">
        <legend><strong>Type</strong></legend>
        <input type="radio" value="gov" name="tType" >Gov
        <input type="radio" value="gfos" name="tType">GFOS
    </fieldset>
    <input type="button" value="Search" name="hfind" Onclick="SubmitForm()">
</form>

The function

function SubmitForm() {
    var hsearch = document.all.submit_Form.hsearch.value;            
    var tType = document.getElementById('tType').value;            
    if ((hsearch = ! '') && (hsearch = ! null)) {               
        if ((tType = ! '') && (tType = ! null)) {
            document.all.submit_Form.submit();
        } else { alert("please select search type");}
    } else { alert("please write search criteria"); }
}

When I run the form the field hsearch is empty, but when I click submit the value will be true, its strange since I still didnt write anything in the input field hsearch, thats why the sels part in JavaScript function will not work

Upvotes: 0

Views: 398

Answers (3)

Amberlamps
Amberlamps

Reputation: 40448

You have to write hsearch != '' instead of hsearch = ! ''.

What you are doing is not checking for a value, but assigning a value that returns true.

hsearch = ! ''
=> hsearch = !''
=> hsearch = true
=> true

Upvotes: 0

Rahul Gupta
Rahul Gupta

Reputation: 10141

Instead of:

var hsearch = document.all.submit_Form.hsearch.value; 

Try:

var hsearch = document.getElementById('hsearch').value; 

You can always debug the value by putting an alert like:

alert(hsearch)

Upvotes: 0

DevelopmentIsMyPassion
DevelopmentIsMyPassion

Reputation: 3591

I think you are using wrong operator. it should be

 if ((hsearch != '') && (hsearch != null)) {               
        if ((tType != '') && (tType != null)) {

Upvotes: 3

Related Questions