驗證表單是否有填資料、填的資料長度是否足夠

<form name="jqueryTest">

用戶名:<input type="text" name="username"/>

密碼:<input type="password" name="password"/>

<button id="submit">送出</button>

</form>

<script>

$('#submit').click(function(){

username=$('input[name=username]').val();

password=$('input[name=password]').val();



if(username.trim().length===0){

alert("請輸入用戶名");

return

}else if(username.trim().length!==3){

alert("用戶名稱需剛好3個字");

return

}else if(password.trim().length===0){

alert("密碼不得空白");

return;

}else{

alert("OK");

}

})

</script>
文章標籤

ivy 發表在 痞客邦 留言(0) 人氣()