-
-
Save mmeganos/0b4804c14be2c7f85ea4296914729526 to your computer and use it in GitHub Desktop.
Restrict user from typing Non-Arabic characters in input fields using Javascript / JQuery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <head></head> | |
| <body> | |
| <form> | |
| <input id="candidate_firstname" name="firstname" required="required" class="gui-input" dir="rtl" type="text" /> | |
| <input id="candidate_lastname" name="lastname" required="required" class="gui-input" dir="rtl" type="text" /> | |
| </form> | |
| <script type="text/javascript"> | |
| function restrictInputOtherThanArabic($field) | |
| { | |
| // Arabic characters fall in the Unicode range 0600 - 06FF | |
| var arabicCharUnicodeRange = /[\u0600-\u06FF]/; | |
| $field.bind("keypress", function(event) | |
| { | |
| var key = event.which; | |
| // 0 = numpad | |
| // 8 = backspace | |
| // 32 = space | |
| if (key==8 || key==0 || key === 32) | |
| { | |
| return true; | |
| } | |
| var str = String.fromCharCode(key); | |
| if ( arabicCharUnicodeRange.test(str) ) | |
| { | |
| return true; | |
| } | |
| return false; | |
| }); | |
| } | |
| jQuery(document).ready(function() { | |
| // allow arabic characters only for following fields | |
| restrictInputOtherThanArabic($('#candidate_firstname')); | |
| restrictInputOtherThanArabic($('#candidate_lastname')); | |
| }); | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment