![](/img/trans.png)
[英]How to check whether a String contains at least one letter using JavaScript Regular Expression?
[英]How to check whether textbox have at least 2 letter OR not in jQuery
我有一個文本框,我想要至少 2 個字母的驗證。
我有以下代碼,用戶可以在其中插入空格並將數據插入數據庫。
但我希望用戶至少輸入 2 個有效字母(字符/數字),然后只有他才能將它們插入到數據庫中。
var data = $("#intials").val();
if (data) {
//OK Inseert value in DB
}
else {
alert('please insert value in intials box.');
}
提前致謝
如果您想確保首字母是字母或數字,並且長度至少為 2 個字符/數字,這里有一種方法。 順便說一句 - 我想也許intials
是一個錯字所以我把它改成了initials
let data = $("#initials").val().trim().replace(/[^0-9a-zA-Z]/gi, '');
if (data.length >= 2) {
// good to go
// data is the value of $("#initials") minus any extra whitespace and without any punctuation
} else {
alert('please insert at least 2 letters (and/or) numbers in the intials box.');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.