Skip to content

Commit 1c7a99d

Browse files
committed
Fixed bug in url validation, added documentation
1 parent 974b303 commit 1c7a99d

File tree

4 files changed

+80
-28
lines changed

4 files changed

+80
-28
lines changed

README.md

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ This plugin was created to minimize javascript logic in the html code when deali
3838
* **validate_length***Validate that input length is in given range (length3-20)*
3939
* **validate_confirmation**
4040
* **validate_spamcheck**
41+
* **validate_ukvatnumber**
4142
* **validate_swesec** - *validate swedish social security number*
4243
* **required***no validation except that a value has to be given*
4344
* **validate_custom** - *Validate value against regexp (validate_custom regexp/^[a-z]{2} [0-9]{2}$/)
@@ -127,6 +128,48 @@ $('#my_form')
127128
});
128129
```
129130

131+
## Localization
132+
All error dialogs can be overwritten by passing an object into the validation function.
133+
134+
```javascript
135+
var jQueryFormLang = {
136+
errorTitle : 'Form submission failed!',
137+
requiredFields : 'You have not answered all required fields',
138+
badTime : 'You have not given a correct time',
139+
badEmail : 'You have not given a correct e-mail address',
140+
badTelephone : 'You have not given a correct phone number',
141+
badSecurityAnswer : 'You have not given a correct answer to the security question',
142+
badDate : 'You have not given a correct date',
143+
toLongStart : 'You have given an answer longer than ',
144+
toLongEnd : ' characters',
145+
toShortStart : 'You have given an answer shorter than ',
146+
toShortEnd : ' characters',
147+
badLength : 'You have to give an answer between ',
148+
notConfirmed : 'Values could not be confirmed',
149+
badDomain : 'Incorrect domain value',
150+
badUrl : 'Incorrect url value',
151+
badFloat : 'Incorrect float value',
152+
badCustomVal : 'You gave an incorrect answer',
153+
badInt : 'Incorrect integer value',
154+
badSecurityNumber : 'Your social security number was incorrect',
155+
badUKVatAnswer : 'Incorrect UK VAT Number'
156+
};
157+
```
158+
159+
```html
160+
<html>
161+
<head>
162+
<script src="scripts/jquery.formvalidator.min.js"></script>
163+
<script src="scripts/locale.en.js"></script>
164+
...
165+
<head>
166+
<body>
167+
...
168+
<form action="script.php" onsubmit="return $(this).validate(jQueryFormLang);">
169+
...
170+
```
171+
172+
130173
## Simple captcha example
131174
```php
132175
<?php
@@ -167,4 +210,8 @@ $_SESSION['captcha'] = array( mt_rand(0,9), mt_rand(1, 9) );
167210
```html
168211
<p>Password: <input type="password" name="pass" data-validation="validate_confirmation" /></p>
169212
<p>Confirm password: <input type="password" name="pass_confirmation" /></p>
170-
```
213+
```
214+
215+
## Contributors
216+
[Matt Clements](https://github.com/mattclements)<br />
217+
[Scott Gonzales](http://projects.scottsplayground.com/iri/)

example.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
99

1010
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
11-
<script type="text/javascript" src="jquery.formvalidator.js"></script>
11+
<script type="text/javascript" src="jquery.formvalidator.min.js"></script>
1212

1313
<link href="style.css" type="text/css" rel="stylesheet"/>
1414

@@ -19,8 +19,8 @@ <h1>
1919
Form validation using jQuery
2020
</h1>
2121
<p>
22-
<a href="https://github.com/victorjonsson/jQuery-Form-Validator/wiki">
23-
https://github.com/victorjonsson/jQuery-Form-Validator/wiki
22+
<a href="https://github.com/victorjonsson/jQuery-Form-Validator/">
23+
https://github.com/victorjonsson/jQuery-Form-Validator/
2424
</a>
2525
</p>
2626

jquery.formvalidator.js

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
* ------------------------------------------
44
* Created by Victor Jonsson <http://www.victorjonsson.se>
55
* Documentation and issue tracking on Github <https://github.com/victorjonsson/jQuery-Form-Validator/>
6-
* Available for download at jQuery.com <http://plugins.jquery.com/project/jQueryFormValidtor/>
76
*
8-
* $license Creative Commons Erkännande-DelaLika 3.0 Unported License <http://creativecommons.org/licenses/by-sa/3.0/>
9-
* $version 1.3.beta
10-
* $stable 1.2 (https://github.com/victorjonsson/jQuery-Form-Validator/zipball/v1.2)
7+
* Dual licensed under the MIT or GPL Version 2 licenses
8+
*
9+
* $version 1.3
1110
*/
1211
(function($) {
1312
$.extend($.fn, {
@@ -35,7 +34,7 @@
3534
* @return {jQuery}
3635
*/
3736
showHelpOnFocus : function(attrName) {
38-
if(typeof attrName === 'undefined') {
37+
if(!attrName) {
3938
attrName = 'data-help';
4039
}
4140

@@ -45,7 +44,7 @@
4544
$(this)
4645
.focus(function() {
4746
var $element = $(this);
48-
if($element.parent().find('.jquery_form_help').length === 0) {
47+
if($element.parent().find('.jquery_form_help').length == 0) {
4948
$element.after(
5049
$('<span />')
5150
.addClass('jquery_form_help')
@@ -78,7 +77,7 @@
7877
* @return {jQuery}
7978
*/
8079
doValidate : function(language, settings, attachKeyupEvent) {
81-
if(typeof attachKeyupEvent === 'undefined') {
80+
if(typeof attachKeyupEvent == 'undefined') {
8281
attachKeyupEvent = true;
8382
}
8483

@@ -219,7 +218,7 @@
219218
//
220219
$form.find('input[type=radio]').each(function() {
221220
var validationRule = $(this).attr(config.validationRuleAttribute);
222-
if (typeof validationRule !== 'undefined' && validationRule === 'required') {
221+
if (typeof validationRule != 'undefined' && validationRule === 'required') {
223222
var radioButtonName = $(this).attr('name');
224223
var isChecked = false;
225224
$form.find('input[name=' + radioButtonName + ']').each(function() {
@@ -242,7 +241,7 @@
242241
if (!ignoreInput($(this).attr('name'), $(this).attr('type'))) {
243242

244243
// memorize border color
245-
if (jQueryFormUtils.defaultBorderColor === null && $(this).attr('type') === 'text') {
244+
if (jQueryFormUtils.defaultBorderColor === null && $(this).attr('type')) {
246245
jQueryFormUtils.defaultBorderColor = $(this).css('border-color');
247246
}
248247

@@ -351,7 +350,7 @@ jQueryFormUtils.validateEmail = function(email) {
351350
var emailFilter = /^([a-zA-Z0-9_\.\-])+@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
352351
if(emailFilter.test(email)) {
353352
var parts = email.split('@');
354-
if(parts.length === 2) {
353+
if(parts.length == 2) {
355354
return jQueryFormUtils.validateDomain(parts[1]);
356355
}
357356
}
@@ -392,9 +391,9 @@ jQueryFormUtils.validateSwedishMobileNumber = function(number) {
392391
number = number.replace(/[^0-9]/g, '');
393392
var begin = number.substring(0, 3);
394393

395-
if (number.length !== 10 && begin !== '467') {
394+
if (number.length != 10 && begin !== '467') {
396395
return false;
397-
} else if (number.length !== 11 && begin === '467') {
396+
} else if (number.length != 11 && begin === '467') {
398397
return false;
399398
}
400399
return (/07[0-9{1}]/).test(begin) || (begin === '467' && number.substr(3, 1) === '0');
@@ -711,8 +710,7 @@ jQueryFormUtils.validateDomain = function(val) {
711710
'nls');
712711

713712
for(var j = 0; j < ukarr.length; j++) {
714-
if(ukarr[j] === tld2)
715-
{
713+
if(ukarr[j] === tld2) {
716714
hasTopDomain = true;
717715
break;
718716
}
@@ -763,7 +761,7 @@ jQueryFormUtils.validateInput = function(el, language, config, form) {
763761
var value = jQuery.trim(el.val());
764762
var validationRules = el.attr(config.validationRuleAttribute);
765763

766-
if (typeof validationRules !== 'undefined' && validationRules !== null) {
764+
if (typeof validationRules != 'undefined' && validationRules !== null) {
767765

768766
/**
769767
* <input data-validation="length12" /> => getAttribute($(element).attr('class'), 'length') = 12
@@ -873,7 +871,7 @@ jQueryFormUtils.validateInput = function(el, language, config, form) {
873871
}
874872

875873
// confirmation
876-
if (validationRules.indexOf('validate_confirmation') > -1 && typeof(form) !== 'undefined') {
874+
if (validationRules.indexOf('validate_confirmation') > -1 && typeof(form) != 'undefined') {
877875
var conf = '';
878876
var confInput = form.find('input[name=' + el.attr('name') + '_confirmation]').eq(0);
879877
if (confInput) {
@@ -901,9 +899,9 @@ jQueryFormUtils.LANG = {
901899
badTelephone : 'You have not given a correct phone number',
902900
badSecurityAnswer : 'You have not given a correct answer to the security question',
903901
badDate : 'You have not given a correct date',
904-
toLongStart : 'You have given an answer longer then ',
902+
toLongStart : 'You have given an answer longer than ',
905903
toLongEnd : ' characters',
906-
toShortStart : 'You have given an answer shorter then ',
904+
toShortStart : 'You have given an answer shorter than ',
907905
toShortEnd : ' characters',
908906
badLength : 'You have to give an answer between ',
909907
notConfirmed : 'Values could not be confirmed',
@@ -924,8 +922,17 @@ jQueryFormUtils.LANG = {
924922
* @return {Boolean}
925923
*/
926924
jQueryFormUtils.validateUrl = function(url) {
927-
var urlFilter = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
928-
return urlFilter.test(url);
925+
// contributed by Scott Gonzalez: http://projects.scottsplayground.com/iri/ but added support for arrays in the url ?arg[]=sdfsdf
926+
var urlFilter = /^(https|http|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|\[|\]|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i;
927+
if(urlFilter.test(url)) {
928+
var domain = url.split(/^https|^http|^ftp/i)[1].replace('://', '');
929+
var domainSlashPos = domain.indexOf('/');
930+
if(domainSlashPos > -1)
931+
domain = domain.substr(0, domainSlashPos);
932+
933+
return jQueryFormUtils.validateDomain(domain); // todo: add support for IP-addresses
934+
}
935+
return false;
929936
};
930937

931938
/**

0 commit comments

Comments
 (0)