最近剛好有這個需求。
所以就用了這個方式來解決。
挺好用的。
有需要的人可以參考,但這個只是做最後的判斷,不是 keyPress 的 Event
/*
 * 輸入字串後,如果是數字或a-z ,A-Z,則回傳 true
 */
function checkVal( str ) {
    var regExp = /^[\d|a-zA-Z]+$/;
    if (regExp.test(str))
        return true;
    else
        return false;
}
參考:
JavaScript Regular Expression (RegExp) in JavaScript
PS.附上 KeyPress 的部份
/*
 * 如果輸入了非0-9, a-z, A-Z,則return false,但用貼上則沒用
 */
$('#pwd').keypress(function(e){
    if((e.shiftKey && e.keyCode == 45) || 
            e.which!=8 && e.which!=0 && !(
            (e.which>=48 && e.which<=57) || 
            (e.which>64 && e.which<91)  || 
            (e.which>=97 && e.which<=122))){
        return false;
    }
});
 
0 comments:
張貼留言