Created
March 13, 2014 23:51
-
-
Save vgrem/9539572 to your computer and use it in GitHub Desktop.
CSR Validation
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
CustomClientValidation = {}; | |
CustomClientValidation.EmailValidator = function() { | |
}; | |
CustomClientValidation.EmailValidator.prototype.Validate = function(value) { | |
value = SPClientTemplates.Utility.Trim(value); | |
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; | |
var hasError = !re.test(value); | |
var errorMsg = hasError ? 'Invalid email address' : ''; | |
return new SPClientForms.ClientValidation.ValidationResult(hasError, errorMsg); | |
}; | |
function EmailField_Edit(rCtx) { | |
if (rCtx == null) | |
return ''; | |
var formCtx = SPClientTemplates.Utility.GetFormContextForCurrentField(rCtx); | |
if (formCtx == null || formCtx.fieldSchema == null) | |
return ''; | |
var _value = formCtx.fieldValue != null ? formCtx.fieldValue : ''; | |
if (_value.length > 0) { | |
var validators = new SPClientForms.ClientValidation.ValidatorSet(); | |
validators.RegisterValidator(new CustomClientValidation.EmailValidator()); | |
formCtx.registerClientValidator(formCtx.fieldName, validators); | |
} | |
return SPFieldText_Edit(rCtx); //default renderer for Text field | |
} | |
(function () { | |
var emailFieldCtx = {}; | |
emailFieldCtx.Templates = {}; | |
emailFieldCtx.Templates.Fields = { | |
'Email': { | |
'NewForm': EmailField_Edit, | |
'EditForm': EmailField_Edit | |
} | |
}; | |
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(emailFieldCtx); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment