Skip to content

Instantly share code, notes, and snippets.

@dixonsiu
Created September 7, 2017 02:28
Show Gist options
  • Save dixonsiu/03f68da9f07913debcae51c08ccd529a to your computer and use it in GitHub Desktop.
Save dixonsiu/03f68da9f07913debcae51c08ccd529a to your computer and use it in GitHub Desktop.
Validate a field and raise error according to length, character types, etc.
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