我正嘗試使用以下條件爲字段創建正則表達式。正則表達式不適用於特殊字符
- 沒有空白應該允許
- 特殊字符不會從下劃線,連字符,期內允許分開。
- 允許使用字母和數字。
我已經創建了自己的正則表達式其工作正常,但它接受在開始
一樣,如果我在場上會工作進入@或$ WER一個特殊字符,數據將被保存。
如果我在該字段中輸入@@,%^ hihf或qwerty @ 333,它將顯示錯誤。
找到以下代碼。
$.formUtils.addValidator({
name: "username",
validatorFunction: function(a) {
return !!a.match((/^[^\s][ A-Za-z0-9_./-]*$/))
},
errorMessage: "Please enter a valid Username (Special characters are not allowed apart from Underscore(_), Hyphen(-) and Period(.)) ",
errorMessageKey: "badname"
}),
@JasshhAndrews你確定嗎?我只是在瀏覽器的JS控制檯中測試了它,它總是在無效輸入中返回「false」,在有效輸入中返回「true」。 –
它的工作。你能解釋這個返回trimmed.length> 0 && rgx.test(trimmed); –
這是一個布爾表達式,用於驗證以下內容:字符串非空,並且修剪後的字符串與正則表達式匹配。 –