[英]regular expression for entering wrong address and restrict user using javascript or jQuery
I tried the piece of code to restrict user if the address is wrongly entered as shown below sample address: #23 78/54, A-31 Nand Jyot Indl Estate, Andheri- Kurla Road, Saki Naka ( allowed )如果地址输入错误,我尝试了一段代码来限制用户,如下所示的示例地址:#23 78/54, A-31 Nand Jyot Indl Estate, Andheri- Kurla Road, Saki Naka(允许)
14/18, Loha Bhavan, PD Mello Rd, Chinch Bunder, mumbai ( allowed ) 14/18,Loha Bhavan,PD Mello Rd,Chinch Bunder,孟买(允许)
only ".....", ",,,,", "----" ( not allowed )只有 ".....", ",,,,", "----" (不允许)
$("#" + (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');
});
I want the user allowed to enter numbers, '#', '/', '-', space, and alphabets only, and if they enter any other special characters it shouldn't allow them to do so.我希望用户只允许输入数字、“#”、“/”、“-”、空格和字母,如果他们输入任何其他特殊字符,则不应允许他们这样做。
code given below is working fine for number with decimal upto two position.下面给出的代码适用于小数点最多两位的数字。 similarly i want to do the same for address.
同样,我想对地址做同样的事情。
below code allows 12345.67下面的代码允许 12345.67
it doesn't allow 23.4565, 23289.499999它不允许 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();
}
I also want the user to enter only alphabets with space and numbers only.我还希望用户只输入带有空格和数字的字母。
sample data: toothpaste 200g ( allowed )样品数据:牙膏200g(允许)
shampoo..( not allowed )洗发水..(不允许)
shampoo (with any other special character like !,?,_,~)( not allowed )洗发水(带有任何其他特殊字符,如 !,?,_,~)(不允许)
Use below code sample.使用下面的代码示例。 Also please find fiddle for same in comment.
也请在评论中找到相同的小提琴。
<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.