/ Published in: Regular Expression
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
checkPhone = '0a635++3Kjhsk9++87$%^&*(100.6'3=-2'; checkPhone.replace(/[^0-9]/g, ''); //returns numbers //result: 06353987100632 checkPhone.replace(/[^\-;\+]/g, ''); //returns + and - //result: ++++- checkPhone.replace(/[0-9]/g, ''); //removes 0-9 //result: a++Kjhsk++$%^&*(.'=- checkPhone.replace(/[0-9\-;\+]/g, '') //removes 0-9 and - and + //result: aKjhsk$%^&*(.'= //------------------------------OR TRY THIS------------------------------------ var error = false; checkPhone = $.trim($('input#phone').val()); stripCheck = checkPhone.replace(/[0-9\-;\+]/g, ''); if(stripCheck.length > 0) //checking for invalid chars (valid: 0-9, +, -) error = true; //console.log('invalid chars found'); numbers = checkPhone.replace(/[^0-9]/g, ''); if(numbers.length < 10) //checking if there is atleast 10 digits error = true; //console.log('more then 10 digits pls'); if(checkPhone.replace(/[^\+]/g, '').length>1 || checkPhone.replace(/[^\-]/g, '').length>1) //there can be only one char of either + or - error = true; //console.log('too many + or -'); if(numbers.substr(0, 2) == '06' && numbers.length != 10) //if 06 it has to have 10 digits error = true; //console.log('06 error'); if(numbers.substr(0, 5) == '00316' && numbers.length != 13) //if 00316 it has to have 13 digits error = true; //console.log('00316 error'); if(checkPhone.substr(0, 4) == '+316' && numbers.length != 11) //if +316 it has to have 11 digits error = true; //console.log('316 error'); console.log(error);