I have textfield to let user to fill his phone number, but I need to force that user to fill it in certain format like this: (0599)-9-111222.
my code is:
function fomratPhone(phone){
for(var i = 0; i < phone.length; i++){
if (phone[0] == "(") continue;
else return false;
if (phone[5] == ")") continue;
else return false;
if (phone[6] == "-" || phone[8] == "-") continue;
else return false;
}
return true;
}
but this code does not work.
Another alternative is the jQuery inputmask plugin
<input type="text" name="phone" id="phone" />
<!-- include jQuery & inputmask plugin -->
<script type="text/javascript">
$(function(){
$("#phone").inputmask("mask", {"mask": "(9999)-9-999999"});
});
</script>
This is just another variation of what NF supplied. I haven't used the one NF mentioned, but I have used this one with no problems. Also, because it's a github project, should you find errors or make corrections you can fork the project and create a pull request with any new additions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With