I created a validation for a dialog in AEM 6.3
Following is the code of my validation.js
(function($, window, document) {
var registry = $(window).adaptTo("foundation-registry");
registry.register("foundation.validation.validator", {
selector: "[data-validation=txt-validate]",
validate: function(el) {
var element = $(el);
console.log(element);
var pattern = element.data('pattern');
var value = element.val();
if (value.length == 0) {
console.log('No validation pattern was mentioned.')
return "Please enter text";
} else {
patterns = {
phone: {
regex: /(\([0-9]{3}\))([0-9]{3})-([0-9]{4})/ ,
format: '(000)000-000',
},
email: {
regex: /((([a-zA-Z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-zA-Z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-zA-Z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-zA-Z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-zA-Z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-zA-Z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-zA-Z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-zA-Z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?/,
format: 'email',
},
}
/*
* Test pattern if set. Pattern can be a preset regex pattern name or
* a regular expression such as "^\\d+$".
*/
if (pattern) {
if (patterns[pattern]) {
error = !patterns[pattern].regex.test(value);
if (error) {
return "The field must match the pattern of " + patterns[pattern].format;
}
} else {
error = !(new RegExp(pattern)).test(value);
if (error) {
return "The field must match the pattern of " + pattern;
}
}
}
}
}
});
})
($, window, document);
I'm getting the following error and the validation is not working.
error: $(...).adaptTo is not a function
the content.xml of my clientlibs folder where validation.js is present is
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="cq:ClientLibraryFolder"
categories="[cq.authoring.dialog, my.category]"/>
Why adaptTo is not a function?
Solved! Go to Solution.
Views
Replies
Total Likes
Added the category as the main category and it worked. not a good answer but works.
Views
Replies
Total Likes
Added the category as the main category and it worked. not a good answer but works.
Views
Replies
Total Likes
Views
Replies
Total Likes
Views
Replies
Total Likes
@Shaheena_Sheikh The problem which I was actually facing was that, clientlib containing jquery was loading after this script hence without jquery all $ functions were not working. By main category I meant the clientlib category were jquery is kept. You can check the sequence of scripts/clientlibs loaded in the <head> tag of the page where you are getting the error. Regarding second comment, If you have created separate clientlib with separate category and the scripts are not getting added then you should probably check if your category is added in footlibs.html of your project.
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies