2015-11-23 43 views
-1

我已經提供了一個jQuery腳本來驗證表單中的電子郵件和密碼。我很不確定如何調用這些函數。任何幫助將不勝感激。如何從窗體調用jquery函數

app.login = { 
     init: function() { 
      $('#login form').submit(function(event) { 
       // call functions here 
      }); 
     } 
    }, 
    app.validation = { 
     email: function(email) { 
      var re = /^(([^<>()[\]\\.,;:\[email protected]\"]+(\.[^<>()[\]\\.,;:\[email protected]\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; 
      return re.test(email); 
     }, 
     password: function(id) { 
      var elementVal = $.trim(id.val()); 
      alert(elementVal) 
      if(elementVal.length > 0) 
       return true; 
     } 
    }; 

我的HTML看起來像這樣:

<form id="login"> 
    <input type="text" class="input-box" id="email"> 
    <input type="password" class="input-box" id="id"> 
    <input class="bottom" type="submit" value="Sign In"> 
</form> 

回答

1

你已經擁有應用程序登錄,如下所示:

app.login = { 
    init: function() { 
     $('#login form').submit(function(event) { 

     }); 

    } 
}, 

因此您已經擁有該活動。現在,你應該實現它,像這樣:

app.login = { 
    init: function() { 
     //fixed selector 
     $('form#login').submit(function(event) { 
      //If either the email or the password is invalid 
      if (!(app.validation.email($("#email").val()) && app.validation.password($("#id").val()))) { 
       //then prevent default event handling, that is, submitting 
       event.preventDefault(); 
       //further error handling 
      } 
     }); 

    } 
}, 
+0

這正是我所期待的。非常感謝 –

+0

我很高興知道您的問題已經解決。 –

1

如果您想在用戶提交表單來執行你的checkigng,您可以使用<form onsubmit="foo()">或聽提交 - 哪個更好 - 事件,如:

$('#myForm').('submit',function(e){ 
    e.preventDefault(); 
    // your work goes here 
}); 

或者你可以d Ø您的input領域的變化檢查,像<input type="text" onchange="myUsernameFunction">,又可以聽這個領域的變化,而不會改變你的input HTML這樣的:

// suppose your password field has id="#psswrdFld" attribute 
$('#psswrdFld').on('input keyup keypress blur change', function(){ 
    // your work here 
} 
0

試試這個

$('#yourform').submit(function() { // your function/code here });