Created
September 7, 2017 02:28
-
-
Save dixonsiu/03f68da9f07913debcae51c08ccd529a to your computer and use it in GitHub Desktop.
Validate a field and raise error according to length, character types, etc.
This file contains 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
function validateCheck(displayNameID, formFieldMsgId) { | |
var displayName = $("#" + displayNameID).val(); | |
var MINLENGTH = 1; | |
var MAXLENGTH = 128; | |
var allowedLetters = /^[0-9a-zA-Z-_]+$/; | |
var lenDisplayName = displayName.length; | |
$("#" + formFieldMsgId).empty(); | |
if(lenDisplayName < MINLENGTH || displayName == undefined || displayName == null || displayName == "") { | |
$("#" + formFieldMsgId).html(i18next.t("create_form.validate.warning.less_minimum_length", { value: MINLENGTH})); | |
return false; | |
} | |
return isCellNameValid(displayName, formFieldMsgId); | |
}; | |
function isCellNameValid(str, formFieldMsgId) { | |
var validCellName = /^([a-zA-Z0-9]([a-zA-Z0-9\-\_]){0,127})?$/g; | |
var MAXLENGTH = 128; | |
var multibyteChar = /[^\x00-\x7F]+/g; | |
var startWithAllowedSymbols = /^[-_]/; | |
if (str.match(validCellName)) { | |
// cell name is valid | |
return true; | |
} else if (str.length > MAXLENGTH) { | |
$("#" + formFieldMsgId).html(i18next.t("create_form.validate.warning.exceed_maximum_length", { value: MAXLENGTH})); | |
return false; | |
} else if (str.match(multibyteChar)) { | |
$("#" + formFieldMsgId).html(i18next.t("create_form.validate.warning.multibyte_not_allowed")); | |
return false; | |
} else if (str.match(startWithAllowedSymbols)) { | |
$("#" + formFieldMsgId).html(i18next.t("create_form.validate.warning.cannot_start_with_symbol")); | |
return false; | |
} else { | |
$("#" + formFieldMsgId).html(i18next.t("create_form.validate.warning.unsupported_symbols")); | |
return false; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment