title: form_for with onsubmit() jQuery function
body: <p>I have this form for user input:</p>
  
  <pre><code>&lt;%= form_for @user, :html =&gt; {:name=&gt;"new_user", :method =&gt; :post, :onclick =&gt; "return validate(this);", :multipart =&gt; true} do |form| %&gt;
  
     &lt;fieldset&gt;   
        &lt;div class="field"&gt;
          &lt;%= form.label :first_name %&gt;
          &lt;%= form.text_field :fist_name %&gt;
        &lt;/div&gt;
  
        &lt;div class="field"&gt;
          &lt;%= form.label :last_name %&gt;
          &lt;%= form.text_field :last_name %&gt;
        &lt;/div&gt;
  
        &lt;div class="field"&gt;
          &lt;%= form.label :email %&gt;
          &lt;%= form.text_field :email %&gt;
        &lt;/div&gt;
  
        &lt;div class="field"&gt;
          &lt;%= form.label :password %&gt;
          &lt;%= form.password_field :password %&gt;
        &lt;/div&gt;
  
        &lt;div class="field"&gt;
          &lt;%= form.label :confirm_password %&gt;
          &lt;%= form.password_field :password_confirmation %&gt;
        &lt;/div&gt;
  
        &lt;div class="field"&gt;
          &lt;%= form.label :country %&gt;
          &lt;%= form.select :country, User::COUNTRY, :prompt =&gt; 'Select your country' %&gt;
        &lt;/div&gt;
  
        &lt;div class="field"&gt;
          &lt;%= form.label :country_code %&gt;
          &lt;%= form.text_field :code, :size=&gt;10 %&gt;
        &lt;/div&gt;
  
  
        &lt;div class="field"&gt;
          &lt;%= form.label :phone_No %&gt;
          &lt;%= form.text_field :phone, :size=&gt;50 %&gt;
        &lt;/div&gt;          
  
        &lt;div class="actions"&gt;
          &lt;%= form.submit 'Submit'%&gt;                 
        &lt;/div&gt;          
      &lt;/fieldset&gt;
    &lt;% end %&gt;
  </code></pre>
  
  <p>Here is my Javascript file called usersvalidations.js</p>
  
  <pre><code>function validate(new_user) {
  var first = new_user.user_fist_name.value;
  var second = new_user.user_last_name.value;
  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var email = new_user.user_email.value;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var password = new_user.user_password.value;
  var passwordconfirm = new_user.user_password_confirmation.value;
  var country = new_user.user_country.value;
  var code = new_user.user_code.value;
  var phone = new_user.user_phone.value;
  
  
  
  
  //name first//  
  if(first==""){
      inlineMsg("user_fist_name", "You must enter your first name.", 8);
      return false;
  }
  if(!first.match(nameRegex)){
      inlineMsg("user_fist_name", "You have entered an invalid name.",8);
      return false;
  }
  
   //name second//
  if(second==""){
      inlineMsg("user_last_name", "You must enter your second name.", 8);
      return false;
  }
  if(!second.match(nameRegex)){
      inlineMsg("user_last_name", "You have entered an invalid name.", 8);
      return false;
  }  
  
   //email//
  if(email ==""){
      inlineMsg("user_email", "&lt;strong&gt; Error &lt;/strong&gt; &lt;br /&gt; You must enter your email", 8);
      return false;       
  }
  if(!email.match(emailRegex)){
      inlineMsg("user_email", "&lt;strong&gt;Error&lt;/strong&gt;&lt;br /&gt; You have entered an invalid email", 8);
      return false;
  }
  
  //password//
  if(password ==""){
      inlineMsg("user_password", "You must provide a password", 8);
      return false;
  }
  
  if(password.length &lt; 6){
      inlineMsg("user_password", "Passwords must be 6 or more characters", 8);
      return false;
  }
  if(passwordconfirm != password){
      inlineMsg("user_password_confirmation", "Password don't match", 8);
      return false;
  }
  
     //country//
  if(country==0){
      inlineMsg("user_country", "You must choose atleast one option", 8)
      return false;
  }
  
      //code//
  
  if(code.length==0){
      inlineMsg("user_code", "You must provide your country code.", 8);
      return false;
  }
  code = code.replace("-","");
  code = code.replace(" ","");
  code = code.replace("(","");
  code = code.replace(")","");
  code = code.replace(".","");
  
  for (i=0; i&lt;code.length; i++) {
      if (code.charAt(i) &lt; "0" || code.charAt(i) &gt; "9") {
          inlineMsg("user_code", "Country code must only contain numbers.", 8);
      return false;
      }
  }   
  
  
     //phone//
  if(phone.length==0){
      inlineMsg("user_phone", "You must provide a phone number.", 8);
      return false;
  }
  phone = phone.replace("-","");
  phone = phone.replace(" ","");
  phone = phone.replace("(","");
  phone = phone.replace(")","");
  phone = phone.replace(".","");
  if (phone.length != 10){
      inlineMsg("user_phone", "Phone numbers must only include a 3-digit area code and a 7-digit phone number.", 8);
      return false;
  }   
  
  for (i=0; i&lt;phone.length; i++) {
      if (phone.charAt(i) &lt; "0" || phone.charAt(i) &gt; "9") {
          inlineMsg("user_phone", "Phone numbers must only contain numbers.", 8);
      return false;
      }
  }
  
  
  return true;
    }
  
  
  // start of message script //
  
   var MSGTIMER = 20;
   var MSGSPEED = 5;
   var MSGOFFSET = 3;
   var MSGHIDE = 3;
  
   // build out the divs, set attributes and call the fade function //
  
     function inlineMsg(target,string,autohide) {
  var msg;
  var msgcontent;
  if(!document.getElementById("msg")){
      msg = document.createElement("div");
      msg.id = "msg";
      msgcontent = document.createElement("div");
      msgcontent.id = "msgcontent";
      document.body.appendChild(msg);
      msg.appendChild(msgcontent);
      msg.style.opacity = 0;
      msg.alpha = 0;
  } else {
      msg = document.getElementById("msg");
      msgcontent = document.getElementById("msgcontent");     
  }
  
  msgcontent.innerHTML = string;
  msg.style.display = "block";
  var msgheight = msg.offsetHeight
  var targetdiv = document.getElementById(target);
  targetdiv.focus();
  var targetheight = targetdiv.offsetHeight;
  var targetwidth = targetdiv.offsetWidth;
  var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
  var leftposition = leftPosition(targetdiv) + targetwidth + MSGOFFSET;
  msg.style.top = topposition + "px";
  msg.style.left = leftposition + "px";
  clearInterval(msg.timer);
  msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
  if(!autohide) {
      autohide = MSGHIDE;
  }
  window.setTimeout("hideMsg()", (autohide*1000));
    }
  
      // hide the form alert //
  
       function hideMsg(msg) {
    var msg = document.getElementById("msg");
     if(!msg.timer) {
      msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
   }
     }
  
     // face the message box //
  
    function fadeMsg(flag){
  if(flag == null){
      flag = 1;
   }
  var msg = document.getElementById("msg");
  var value;
  if(flag == 1){
      value = msg.alpha + MSGSPEED;
  } else {
      value = msg.alpha - MSGSPEED;
  }
  msg.alpha = value;
  msg.style.opacity = (value / 100);
  msg.style.filter = "alpha(opacity=" + value + ")";
  if(value &gt;= 99){
      clearInterval(msg.timer);
      msg.timer = null;
  } else if(value &lt;=1){
      msg.style.display = "none";
      clearInterval(msg.timer);
  }
  }
  
   // calculate the position of the element in relation to the left of the browser //
  
     function leftPosition(target) {
   var left = 0;
   if(target.offsetParent){
      while(1) {
          left += target.offsetLeft;
          if(!target.offsetParent){
              break;
          }
          target = target.offsetParent;
      }
  } else if(target.x) {
      left += target.x;
  } 
  return left;
      }
  
       // calculate the position of the element in relation to the top of the browser window //
  
    function topPosition(target){
  var top = 0;
  if(target.offsetParent){
      while(1){
          top += target.offsetTop;
          if(!target.offsetParent) {
              break;
          }
          target = target.offsetParent;
      }
  } else if(target.y) {
      top += target.y;
  }
  return top;
      }
  
     // preload the arrow //
  
     if (document.images) {
  arrow = new Image(7,80);
  arrow.src = "msg_arrow.gif";
      }
  </code></pre>
  
  <p>email and password are not working well, they don't show the validation message and neither are they validating.</p>
  
  <p>Need your assistance.</p>