我想驗證使用JavaScript的輸入,我想要的是如果用戶輸入一些未包括在我的數組中的提示警報。JS檢查輸入是否在數組範圍內
這裏是我迄今爲止
var validAr = ["1","2","3","4","5","6","7","8","9","10","m","M"];
//dont mind the i it's been previously declared
val =+ document.getElementById('arrow_' + i + '_1').value
alert(val);
alert(validAr.indexOf(val));
例如,如果在用戶上面進入「5」,我會提醒「5」的val
然後將值-1,這意味着它不屬於數組。我該如何解決這個問題?
我想你最好使用正則表達式進行這種驗證。這裏是一個有用的參考http://tech.pro/tutorial/1214/javascript-regular-expression-enlightenment – Strikers