Skip to content

Added simple phone number validation #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ <h2>Demo</h2>
<label>E-mail (required)</label>
<input type="email" name="email" required />
</div>
<div class="entry">
<label>Phone number (required)</label>
<input type="tel" name="tel" required />
</div>
<div class="entry">
<label>
<input type="checkbox" name="newsletter" value="1" required />
Expand Down
13 changes: 11 additions & 2 deletions jquery.html5validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
requiredCheckbox: 'This field is required',
minlength: 'The minimum length of this field is not met',
maxlength: 'The content of this field is too long',
email: 'This is not an email address'
email: 'This is not an email address',
tel: 'This is not an phone number'
},
customValidators: {},
fieldParentSelector: '.entry'
Expand Down Expand Up @@ -64,6 +65,7 @@
this.validate( 'minlength', $('input[data-minlength]', this.element) );
this.validate( 'maxlength', $('input[data-maxlength]', this.element) );
this.validate( 'email', $('input[type=email]', this.element) );
this.validate( 'tel', $('input[type=tel]', this.element) );
}

// validation rules set in the config
Expand Down Expand Up @@ -149,6 +151,13 @@
email: function( el ) {
var regEx = /^([\w-\.\+]+@([\w-]+\.)+[\w-]{2,4})?$/;
return regEx.test( $(el).val() );
},
tel: function( el ) {
if ($(el).val().length == 0) {
return true;
}
var regEx = /^[\+]?[\d]{9,13}$/;
return regEx.test( $(el).val() );
}
};

Expand Down Expand Up @@ -206,4 +215,4 @@
});
}

})( jQuery, window, document );
})( jQuery, window, document );