Jayyrus
Jayyrus

Reputation: 13051

Input type image submit form without my consense

i've an input type image like this:

<input type="image" name="formimage2" width="170" height="37" src="images/tasto-registrati.png" onclick="sub()">

this is sub():

function sub(){
            var mail = document.getElementsByName('mail')[0];
            var name = document.getElementsByName('name')[0];
             if(mail.value!=""){
                bool = true;
            }else{
                mail.value="Campo obbligatorio";
                mail.style.backgroundColor="red";
                bool=false;
            }
            if(name.value!=""){
                bool = true;
            }else{
                name.value="Campo obbligatorio";
                name.style.backgroundColor="red";
                bool=false;
            }
if(bool){
   document.form[0].submit();
}

but form submit everytime! how can i tell to form to submit only in that case? i try:

if(bool){
...
}else{
   return false
}

but nothing! can you help me??

Upvotes: 2

Views: 1828

Answers (3)

Fahim Parkar
Fahim Parkar

Reputation: 31637

Just use

onclick="return sub()"

I believe your form will not get submitted when it is FALSE...

Good Luck!!!

Update 1:

always use return false or return true in main function itself...

javascript part

function checkMe() {

if (Name is incorrect) {
alert(Name is incorrect);
return false;
}


if (Number is incorrect) {
alert(Number is incorrect);
return false;
}
return true;
}

html part

<input type=submit onClick="return checkMe()">

Upvotes: 5

Diode
Diode

Reputation: 25145

<input type="image" name="formimage2" width="170" height="37" src="images/tasto-registrati.png" onclick="sub(event)">

function sub(e){

  ...
  ...
  if(!bool){
     e.preventDefault();
  }else{
     ......
  }


}

Upvotes: 0

Riz
Riz

Reputation: 10246

You can try onsubmit="return sub()" for form tag.

Also you should return bool; Form will be submitted if bool is true.

Upvotes: 1

Related Questions