[英]Javascript Regular Expression to restrict user from Entering same consecutive digits in a text field
[英]regular expression for entering wrong address and restrict user using javascript or jQuery
如果地址輸入錯誤,我嘗試了一段代碼來限制用戶,如下所示的示例地址:#23 78/54, A-31 Nand Jyot Indl Estate, Andheri- Kurla Road, Saki Naka(允許)
14/18,Loha Bhavan,PD Mello Rd,Chinch Bunder,孟買(允許)
只有 ".....", ",,,,", "----" (不允許)
$("#" + (idOfElement)).on("keypress keyup", function (e) {
this.value = this.value
//.replace(/[^\..]/g,'')
.replace(/(^[\d#\w]+)/g, '$1')
.replace(/(\s[1-9A-Za-z\/#-]*)./g, '$1');
});
我希望用戶只允許輸入數字、“#”、“/”、“-”、空格和字母,如果他們輸入任何其他特殊字符,則不應允許他們這樣做。
下面給出的代碼適用於小數點最多兩位的數字。 同樣,我想對地址做同樣的事情。
下面的代碼允許 12345.67
它不允許 23.4565, 23289.499999
//for numbers with decimal point upto two position
$("#" + (idOfElement)).on("keypress keyup blur", function (e) {
this.value = this.value
.replace(/[^\d.]/g, '')
.replace(/(^[\d]+)([\d]*)/g, '$1')
.replace(/(\..*)\./g, '$1')
.replace(/(\.[\d]{2})./g, '$1');
if ((e.which != 46 || $(this).val().indexOf('.') != -1) && (e.which < 48 || e.which > 57)) {
e.preventDefault();
}
我還希望用戶只輸入帶有空格和數字的字母。
樣品數據:牙膏200g(允許)
洗發水..(不允許)
洗發水(帶有任何其他特殊字符,如 !,?,_,~)(不允許)
使用下面的代碼示例。 也請在評論中找到相同的小提琴。
<input type="text" id="addressID">
$(document).ready(function(){
$("input").bind('input', function(e) {
var str = $("#addressID").val();
var re = new RegExp(/^[ A-Za-z0-9\/#-]*$/g);
if (re.test(str)) {
//alert('valid');
} else {
alert("Invalid address, only alphanumeric values and '/','#','-' allowed");
$("#addressID").val($("#addressID").val().slice(0, -1));
}
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.