Skip to content

Commit 73d5b9e

Browse files
committed
add email and url validators
1 parent c28578d commit 73d5b9e

File tree

6 files changed

+103
-2
lines changed

6 files changed

+103
-2
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ This validator can take options as object with property `min`, e.g:
141141
```js
142142
validators: {numeric: {min:0}}
143143
```
144+
Available validators:
145+
- required
146+
- email
147+
- url
144148
145149
##### Events
146150
Formus emit an event after validation:

app/js/formus.js

+34
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,40 @@ formus.provider('FormusValidator', function($logProvider) {
728728
return config.label + ' cannot be blank';
729729
}
730730
return null;
731+
},
732+
email: function(value, config) {
733+
var validateEmail = function(value) {
734+
if (value) {
735+
var pattern = /^(([^<>()[\]\\.,;:\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,}))$/;
736+
return pattern.test(value);
737+
} else {
738+
return true;
739+
}
740+
}
741+
742+
if (!validateEmail(value)) {
743+
return config.label + ' must be a valid e-mail';
744+
}
745+
return null;
746+
},
747+
url: function(value, config) {
748+
var validateLink = function(url) {
749+
if (value) {
750+
var pattern = new RegExp('^(https?:\\/\\/)?' + // protocol
751+
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name
752+
'((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address
753+
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path
754+
'(\\?[;&a-z\\d%_.~+=-]*)?' + // query string
755+
'(\\#[-a-z\\d_]*)?$', 'i'); // fragment locator
756+
return pattern.test(url);
757+
} else {
758+
return true;
759+
}
760+
}
761+
if (!validateLink(value)) {
762+
return config.label + ' must be a valid url';
763+
}
764+
return null;
731765
}
732766
};
733767
var getProvider = function($log) {

bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "angular-formus",
33
"description": "Form generator for AngularJS",
4-
"version": "0.0.5",
4+
"version": "0.0.6",
55
"license": "MIT",
66
"keywords": [
77
"angular",

dist/formus.js

+29
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,35 @@
702702
return config.label + ' cannot be blank';
703703
}
704704
return null;
705+
},
706+
email: function (value, config) {
707+
var validateEmail = function (value) {
708+
if (value) {
709+
var pattern = /^(([^<>()[\]\\.,;:\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,}))$/;
710+
return pattern.test(value);
711+
} else {
712+
return true;
713+
}
714+
};
715+
if (!validateEmail(value)) {
716+
return config.label + ' must be a valid e-mail';
717+
}
718+
return null;
719+
},
720+
url: function (value, config) {
721+
var validateLink = function (url) {
722+
if (value) {
723+
var pattern = new RegExp('^(https?:\\/\\/)?' + '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + '((\\d{1,3}\\.){3}\\d{1,3}))' + '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + '(\\?[;&a-z\\d%_.~+=-]*)?' + '(\\#[-a-z\\d_]*)?$', 'i');
724+
// fragment locator
725+
return pattern.test(url);
726+
} else {
727+
return true;
728+
}
729+
};
730+
if (!validateLink(value)) {
731+
return config.label + ' must be a valid url';
732+
}
733+
return null;
705734
}
706735
};
707736
var getProvider = function ($log) {

0 commit comments

Comments
 (0)