In here I want to know how to add these symbols + - ( ) for this validation. Currently this section only accept numbers. I want accept both number and these sysmbols + - ( ) . Here is my code
if (typeof !fields["enterprisemobile"]) {
isValid = false;
var pattern = new RegExp(/^[0-9\b]+$/);
if (!pattern.test(fields["enterprisemobile"])) {
isValid = false;
errors["enterprisemobile"] = "Mobile Number is invalid";
}
}
Replace your regex to accept new symbols. Something like:
if (typeof !fields["enterprisemobile"]) {
isValid = false;
var pattern = new RegExp(/^[0-9\b\+\-\(\)]+$/);
if (!pattern.test(fields["enterprisemobile"])) {
isValid = false;
errors["enterprisemobile"] = "Mobile Number is invalid";
}
}
If you want a more accurated explanation of what this regex means you could go here.
Related
I want to make restriction on mobile number and I have to form contain mobile number, I want to set one of these input that match the constraint in var mobileInput and make some validation on it, but I can't make || why? And how I can make this?
var mobileInput = input.attr('id') === "mobile-number" || input.attr('id') === "student_mobile";
if (mobileInput) {
return validateMobileInput(input, formGroup);
function validateEmailInput(input, formGroup) {
var emailRegex = /^[a-zA-Z0-9._-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i;
var validEmail = emailRegex.test(input.val());
if (validEmail) {
successInput(formGroup);
} else {
errorInput(formGroup, "Email is incorrect");
}
return validEmail;
}
Requirement : To validate password and emailID entered by user.
I have designed a dialog for user to enter there email id and password for creating their new account.
I want the the user input to be validated on the "next" button of the dialog.
I have written a JavaScript for it as shown below and added a custom action in "do action" of my dialog button.
function validatePassword(str szPasswordportal)
{
var newPassword = szPasswordportal;
var minNumberofChars = 6;
var maxNumberofChars = 20;
var regularExpression = /^[A-Za-z0-9`~!#%]{6,20}$/;
alert(newPassword);
if(newPassword = "") //if null
return false;
if(newPassword.length < minNumberofChars || newPassword.length > maxNumberofChars)
{
return false;
}
if(!regularExpression.password(newPassword))
{
alert("password should contain atleast one number ,one alphabet and one special character");
return false;
}
return true;
}
But this JS is not getting executed successfully.
Can someone help me out with this or with some other suggestion?
Your if condition have a syntax mistake.
if(newPassword = "")
= is assigning operator. If you want to check the value you have to use conditional operator == like below.
if(newPassword == "")
Also you have to add all the condition on else part, then only it will check the validation one by one, otherwise at the end it will automatically return the true value. Change your script like below.
function validatePassword(str szPasswordportal)
{
var newPassword = szPasswordportal;
var minNumberofChars = 6;
var maxNumberofChars = 20;
var regularExpression = /^[A-Za-z0-9`~!#%]{6,20}$/;
alert(newPassword);
if(newPassword == "" || newPassword.length < minNumberofChars || newPassword.length > maxNumberofChars)
{
return false;
} else if(!regularExpression.password(newPassword))
{
alert("password should contain atleast one number ,one alphabet and one special character");
return false;
} else {
return true;
}
}
I need to make a new method for jQuery Validator and don't know where to start.
I would like it check that the email entered includes: '#specificdomain.com'.
But that it is also the very last part of the input. For example #specificdomain.comChris would not do.
<script type="text/javascript">
jQuery.validator.addMethod("mustinclude", function(value, element) {
return this.optional(element) || value == ?
}, "must include #specificdomain.com at the end of the text input");
$(document).ready(function(){ .....
So far I've only come across value == value.match(), hence this is where I've got stuck.
Cheers Chris
jQuery.validator.addMethod('matchDomain', function(value, element) {
var s=value;
var split = s.split('#');
var regex = /^([a-zA-Z0-9_.+-])+$/;
var s2="#allcoles.com";
var optionalValue = this.optional(element);
if (optionalValue) {
return optionalValue;
}
if(regex.test(split[0]) && s2.equals(split[1]))
{
return true;
}
else
{
return false;
}
}, 'Please specify a #allcoles.com email');
The following worked for me:
jQuery.validator.addMethod('matchDomain', function(value, element) {
var s=value;
var split = s.split('#');
var regex = /^([a-zA-Z0-9_.+-])+$/;
**var s2="allcoles.com";** //The split array is the domain excluding the #
**var optionalValue = this.optional(element);** //This is how other methods in alternativeMethods.js Validator handle this.
**//Debugging - This is useful to see visually what is happening
//alert(split[0]); // Shows the inputted username i.e chris or smokey
//alert(split[1]); // Shows the inputted domain
//alert(regex.test(split[0])); //Shows unfilled inputs problem or bad characters, true if good, false if bad
//alert(s2 == split[1]);** // Shows if the inputted domain matches variable s2, if it does we get a true
if (optionalValue) {
return optionalValue;
}
**if(regex.test(split[0]) && (s2 == split[1]))** // has to be == not equals
{
return true;
}
else
{
return false;
}
}, 'Please specify a #allcoles.com email');
var s="abc#specificdomain.com"; OR var s=value;
var split = s.split('#');
var regex = /^([a-zA-Z0-9_.+-])/;
var s2="#specificdomain.com";
if(regex.test(split[0]) && s2 == split[1])
return true;
else
return false;
Reg.js
function chkform() {
var pattern = "/^([a-zA-Z0-9_\.\-])+\#(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/";
var ret = true;
if (document.getElementById("name").value === "") {
document.getElementById("name").value = 'Name cannot be left blank' ;
//document.getElementById("name").style.color="red" ;
ret = false;
}
if (document.getElementById("email").value != "") {
if (document.getElementById("email").value != pattern ) {
document.getElementById("email").value = 'format is not right' ;
//document.getElementById("email").style.color="red" ;
ret = false;
}
} else {
document.getElementById("email").value = 'cannot b left blank' ;
}
if (document.getElementById("number").value === "") {
document.getElementById("number").value = 'number cannot be left blank' ;
//document.getElementById("number").style.color="red" ;
ret = false;
}
if (document.getElementById("city").value === "") {
document.getElementById("city").value = 'city cannot be left blank' ;
//document.getElementById("city").style.color="red" ;
ret = false;
}
if (document.getElementById("dep").value === "") {
//console.log("5");
ret = false;
}
if (document.getElementById("cname").value === "Course interested in") {
console.log("6");
ret = false;
}
return ret;
}
I am validating my form using reg.js , when the values are empty It works fine but when i enter my mail id akash.bhardwaj#gmail.com , acc to my code its show the format is not right but the format is right because my email is right so where I am going wrong and i want to check for phone also
This is your problem:
if (document.getElementById("email").value != pattern ) {
Since you're using a regular expression, you want to do a test on the pattern, not a string comparison. To make things easier, you can use a regular expression literal; so you would write instead:
var pattern = /^([a-zA-Z0-9_\.\-])+\#(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
Note that I removed the quotes. And then change the other line to:
if ( pattern.test( document.getElementById("email").value ) ) {
More info can be found here: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions
Another, more modern/HTML5 way might be to just make the input type="email" and have a custom validation handler. For more info on that, see https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML5/Constraint_validation .
(I'll put it as an answer rather than a comment if it's helpful)
Regex for email validation is a minefield. W3 are proposing a type=email attribute for html that uses:
/^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+#[a-zA-Z0-9-]+(?:.[a-zA-Z0-9-]+)*$/
It's a pretty good pattern, but there are so many possibilities in email that it can be difficult to be 100% accurate.
http://dev.w3.org/html5/markup/input.email.html
I want to validate input as I type so I use onkeyup event to do so, but if I validate an email regex "name#domain.com", as soon as user starts to type it throws an error - first character doesnt match regex...
So I wrote this:
var addValidation = function (patterns) {
var index = patterns.length; //I know I can avoid this
while (index--) {
patterns[index] = new RegExp(patterns[index]);
}
index = 0;
var current = patterns[index],
matchExact = function (patt, str) {
var match = str.match(patt);
return match !== null && str === match[0];
};
return function () {
var str = this.value;
if (!matchExact(current, str) ) {
var tmp = patterns[index + 1] ?
new RegExp(current.source + patterns[index + 1].source) :
false;
if (tmp && matchExact(tmp, str)) {
current = tmp;
index++;
}
else {
alert("Wrong");
}
}
}
};
document.getElementById("x").onkeyup = addValidation(["[a-zA-Z0-9\\.]+", "#{1}", "[a-zA-Z0-9]+", "\\.{1}", "[a-zA-Z]{1,3}"]);
It seems to work, but... it's ugly and it will alert you if you do step back (eg. "name#" and you press backspace).
I know that Dojo's validation is great, but I do not want to use Dojo. Are there any better ways to achieve that?
//EDIT: http://livedocs.dojotoolkit.org/dijit/form/ValidationTextBox this is an example, but you can define your own pattern (like email regex) and it will validate it perfectly.
Add interval before validation will start:
var t;
document.getElementById("x").onkeyup = function () {
if (t) {
clearTimeout(t);
}
t = setTimeout(function () {
//do validation
}, 1000)
}
Don't ever try to validate an email address with a regualr expression. You'll either end up allowing addresses which are not valid, or block email addresses which are perfectly valid and just annoy your visitors. It's also worth bearing in mind that the best regex so far for validating email addresses is this:
http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html