Skip to content

Instantly share code, notes, and snippets.

@qrobin
Created December 17, 2016 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qrobin/15c19d96a7b3756b3efdd3ba821bf7ef to your computer and use it in GitHub Desktop.
Save qrobin/15c19d96a7b3756b3efdd3ba821bf7ef to your computer and use it in GitHub Desktop.
;
(function($, window, document, undefined) {
"use strict";
var methods = {
codes: {
'a': 0x0627,
'b': 0x0628,
'c': 0x0686,
'd': 0x062F,
'e': 0x0639,
'f': 0x0641,
'g': 0x06AF,
'h': 0x06BE,
'i': 0x06CC,
'j': 0x062c,
'k': 0x06A9,
'l': 0x0644,
'm': 0x0645,
'n': 0x0646,
'o': 0x06C1,
'p': 0x067E,
'q': 0x0642,
'r': 0x0631,
's': 0x0633,
't': 0x062A,
'u': 0x0626,
'v': 0x0637,
'w': 0x0648,
'x': 0x0634,
'y': 0x06D2,
'z': 0x0632,
'A': 0x0622,
'C': 0x062B,
'D': 0x0688,
'E': 0x0639,
'F': 0x064D,
'G': 0x063A,
'H': 0x062D,
'I': 0x0670,
'J': 0x0636,
'K': 0x062E,
'L': 0x0628,
'M': 0x064B,
'N': 0x06BA,
'O': 0x06C3,
'P': 0x064F,
'Q': 0x0656,
'R': 0x0691,
'S': 0x0635,
'T': 0x0679,
'U': 0x0631,
'V': 0x0638,
'W': 0x0634,
'X': 0x0698,
'Y': 0x0601,
'Z': 0x0630
},
/**
* __constructor method
* @param {} user defined options
*/
init: function(options) {
var el = this;
/**
* save options
*/
options = $.extend(true, {}, $.fn.urdu.defaults, options);
el.data('options', options);
},
/**
* Attachs validator to form.submit and field.blur events
*/
attach: function() {
this.on("keydown", methods._onKeydown);
return this;
},
/**
* Called on keydown
*
* @return
*/
_onKeydown: function(e) {
var el = $(this),
key = e.key,
urduCode = methods._urduKey(e, key),
cursorPosition = el.prop("selectionStart"),
val = el.val();
if (key === 'shiftKey' || key === 'Control') {
return;
}
if (urduCode) {
el.val([val.slice(0, cursorPosition), urduCode, val.slice(cursorPosition)].join(''));
methods._setCaretPosition(el, cursorPosition + 1)
event.preventDefault();
}
return true;
},
_setCaretPosition: function(el, caretPos) {
if (el.createTextRange) {
var range = el.createTextRange();
range.move('character', caretPos);
range.select();
} else {
if (el.selectionStart) {
el.focus();
el.setSelectionRange(caretPos, caretPos);
} else
el.focus();
}
},
_urduKey: function(evt, key) {
if (evt.ctrlKey || evt.altKey || !!!methods.codes[key]) {
return false;
}
return String.fromCharCode(methods.codes[key]);
}
}
$.fn.urdu = function() {
this.each(function() {
var el = $(this);
methods.init.apply(el, arguments);
return methods.attach.apply(el);
});
}
$.fn.urdu.defaults = {
onSuccess: false,
onFailure: false,
InvalidFields: [],
showPrompt: true,
validateNonVisibleFields: true,
scroll: true,
focusFirstField: true,
validationAttribute: 'validationtype',
errorClass: 'error',
scrollSpeed: 300,
groups: {}
}
})(jQuery, window, document); //friehapapamama
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment