javascript - Open new window if there are no problems with from validation -


i have page validates basic form info. after user entered info , if there no problems validation there should open new window onclick. if there problems form fields user should prompted fix wrong , window should not open. have attempted write code problem have window opens regardless whether there problem form or not. window should open if there no problems form.

would appreciate if point me in right direction can solve little problem.

code follows:

var name = document.getelementbyid("name").value; var phone = document.getelementbyid("phone").value; var nat = document.getelementbyid("nat").value; var address = document.getelementbyid("address").value; var town = document.getelementbyid("town").value; var zip = document.getelementbyid("zip").value; var username = document.getelementbyid("username").value;   if (name == "" ) { window.alert("please enter full name");     }   checknr= isnan(phone) if(checknr == true) { window.alert("phone number: can enter numbers. please try again"); }  else if (phone == "") { window.alert("please enter phone number")  ; }  if (nat == "") { window.alert("please enter nationality") }   if(town =="") { window.alert("please enter town") }  if(zip=="") { window.alert("please enter zip code") }   if (username=="") { window.alert("please enter username") }  } </script> 

html

<form name="subscribe" action="mailto:" > fullname: </strong><input type="text"  id="name"/><br />   phone nr: <input type="text" id="phone" /><br />   nationality:<input type="text" id="nat" /><br />   address:<input type="text" id="address" /><br />   town:<input type="text" id="town" /><br />   zip code: <input type="text" id="zip" /><br />  username: <input type="text" id="username" /><br />  date of lesson:<input type="text" id="dol" value="dd/mm/yy" />    <input type="button" value="submit" onclick="validate(), openwindow()" />  </form> 

right now, validate , openwindow written sequentially, , execute regardless of either's outcome.

what can this.

function validate() {     //if validation fails, return false, else return true. } 

change onclick attribute to

onclick="if(validate()) openwindow();" 

now, if validation goes through successfully, new window opened. if not, no bueno.


Comments