could you please tell me how to combine regex in jquery ? .I have function to validate mobile number.But I am using three (3) regex I want to combine to one (1). can we do that
here is my code https://jsbin.com/japokekiji/edit?html,js,console,output
valid number
+919313854539
9313854539
09313854539
$(function(){
var _const ={
validMobileNumber: ['7777777777', '8888888888', '9999999999']
}
$('input').on('keyup',function(){
console.log(validMobileNumber($(this).val()));
})
function validMobileNumber(mobileNumber) {
var regWithoutZero = /^([7-9])[0-9]{9}$/;
var regWithZero = /^(0)[7-9][0-9]{9}$/;
var regWithCountryCode = /^(\+91)[7-9][0-9]{9}$/;
var isValid = false;
if (mobileNumber != "" && mobileNumber != null) {
if (_const.validMobileNumber.indexOf(mobileNumber)!=-1) {
isValid = false;
}
else if (regWithoutZero.test(mobileNumber) || regWithZero.test(mobileNumber) || regWithCountryCode.test(mobileNumber)) {
isValid = true;
}
else {
isValid = false;
}
}
return isValid;
}
})
Well it depends, for instance if you want to test with an OR operator, this would be something like this :
var mergedRegex = /^(([7-9])[0-9]{9}|(0)[7-9][0-9]{9}|(\+91)[7-9][0-9]{9})$/;
If you want with an AND operator, try this instead :
var mergedRegex = /^(?=([7-9])[0-9]{9})(?=(0)[7-9][0-9]{9})(?=\+91)[7-9][0-9]{9})$/;
I would go with :
var mergedRegex = /^(\\+91|0)?[7-9][0-9]{9}$/
I would advise reading up here , if you want to learn all there is to know about regexes.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.