Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force user to fill phone textfield in certain format

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.

like image 536
Ayman Hussein Avatar asked Jun 13 '13 12:06

Ayman Hussein


1 Answers

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.

like image 121
Brad Christie Avatar answered Oct 30 '22 08:10

Brad Christie