Skip to content

Instantly share code, notes, and snippets.

@mahedi2014
Last active September 2, 2015 08:15
Show Gist options
  • Save mahedi2014/5332731e70ccb1c6f388 to your computer and use it in GitHub Desktop.
Save mahedi2014/5332731e70ccb1c6f388 to your computer and use it in GitHub Desktop.
From validation
/**
* Make capital on input
*
* @param fieldId
* @param thisValue
*
* Example: onkeypress="return inputCapital(event, this.id, 100);"
*/
function inputCapital(fieldId, thisValue){
$('#'+fieldId).val( $('#'+fieldId).val().toUpperCase() );
}
/**
* Allow only alphabet only
*
* @param evt
* @param fieldId
* @param maxLength
* @returns {boolean}
*
* Example: onkeypress="return onlyAlphabet(event, this.id, 100);"
*/
function onlyAlphabet(evt, fieldId, maxLength) {
$("#"+fieldId).attr('maxlength', maxLength);
evt = (evt) ? evt : window.event;
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (evt.shiftKey || evt.ctrlKey || evt.altKey) {
evt.preventDefault();
} else {
var key = evt.keyCode;
if (!((key == 8) || (key == 32) || (key == 46) || (key >= 35 && key <= 40) || (key >= 65 && key <= 90))) {
evt.preventDefault();
}
}
return true;
}
/**
* Validation for only number input
*
* @param evt
* @param fieldId
* @param maxLength
* @param alertStatus, true for alert on
* @returns {boolean}
*
* Example: onkeypress="return isNumber(event, this.id, 100);"
*/
function isNumber(evt, fieldId, maxLength, alertStatus) {
$("#"+fieldId).attr('maxlength', maxLength);
evt = (evt) ? evt : window.event;
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
if(alertStatus){
alert("Only number allowed.");
}
return false;
}
return true;
}
/**
* Allow only float as amount
*
* @param evt
* @param fieldId
* @param maxLength
* @returns {boolean}
*
* Example: onkeypress="return isFloat(event, this.id, 100);"
*/
function isFloat(evt, fieldId, maxLength) {
$("#"+fieldId).attr('maxlength', maxLength);
evt = (evt) ? evt : window.event;
if ((evt.which != 46 || $('#'+fieldId).val().indexOf('.') != -1) && (evt.which < 48 || evt.which > 57)) {
evt.preventDefault();
}else{
return true;
}
}
/**
* Validation for mobile number input
*
* @param evt
* @param fieldId
* @param maxLength
* @param alertStatus, true for alert on
* @returns {boolean}
*
* Example: onChange="isMobile(evt, 'fieldId', '11', true)"
*/
function isMobile(evt, fieldId, maxLength, alertStatus){
var mobileNo = $('#'+fieldId).val();
var mobileLength = mobileNo.length;
if( mobileLength > maxLength ){
if(alertStatus){
alert("Mobile number can not be more than "+maxLength+" digits.");
}
return false;
}else if(!isNumber(evt, fieldId, maxLength, false)){
if(alertStatus){
alert("Invalid key press.");
}
return false;
}else{
return true;
}
}
function validateEmail(thisValue) {
var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
return re.test(thisValue);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment