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;
}
}
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;
}
I want to validate this text field email which contains Multiple emails; all separated by semi colons but this isn't working. It should break when msn.com have been entered along with other emails.
var isMsn = ["msn.com"];
function myFunction() {
var str = email.value;
//var isMsn = str.includes("msn.com");
var res = str.split(";");
var isValid = false;
//alert(res.length);
for (var i = 0; i < res.length; i++){
//alert(res[i]);
var x = res[i];
//check to see if this is an valid emAIL then
var mailformat = /^\w+([\.-]?\w+)*#\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if(x.match(mailformat)){
alert("valid email address!");
//IF isMsn = TRUE then check to see if is contains msn.com
if (x.contains(isMsn) ){
alert("it is MSN");
//res[+1];
isValid = true;
}
else {
alert('no MSN');
if (res.includes(isMsn)){
//alert('Cannot mix MSN emails with other');
//isValid = false;
//res[-1];
break;
}
//else{
//alert('it is other');
//isValid = true;
//}
}
}
else{
alert("You have entered an invalid email address!");
isValid = false;
}
}
}
I want it to loop through and all the emails must either match a msn.com or other NOT Both. for example, user#msn.com;user1#msn.com or user#hotmail.com;user1#hotmail.com but NOT user#msn.com;user1#hotmail.com
Not sure if .includes is the best option or not!
1) You should use includes instead of contains;
2) The includes or contains method accepts a string, not an array, so it should be res.includes(isMsn[0]);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes
And if you want to validate by multiple domains - you should not name it isMsn, but prohibitedDomains and then to Array.some (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some) over this array.
prohibitedDomains.some((domain) => res.includes(domain))
I have a piece of HTML that creates a web form with three text fields (name, group and number), all of which are validated using JavaScript to check that there is data inputted into them. In the last text field, I need to introduce an additional bit of JavaScript to check that the data inputted by the user is also four digits long (for example 2947 or 94Q3). As a complete JavaScript novice, I'm not sure how I would do this! Would I have to create a variable that could take the value of the inputted data, then count the digits of the variable, or could I do it directly from the field? Here is the Javascript section of my code:
function validateForm() {
var result = true;
var msg = ””;
if (document.Entry.name.value == ””) {
msg += ”You must enter your name\n”;
document.Entry.name.focus();
document.getElementById(‘name’).style.color = ”red”;
result = false;
if (document.Entry.group.value == ””) {
msg += ”You must enter the group\n”;
document.Entry.group.focus();
document.getElementById(‘group’).style.color = ”red”;
result = false;
}
if (document.Entry.number.value == ””) {
msg += ”You must enter the number\n”;
document.Entry.number.focus();
document.getElementById(‘number’).style.color = ”red”;
result = false;
}
if (msg == ””) {
return result;
} {
alert(msg)
return result;
}
}
If possible, could you tell me what code I would need to insert? Thank you!
Place this block in your conditions list:
if (document.Entry.number.length!=4) {
msg+=”You must enter 4 digits \n”;
document.Entry.number.focus();
document.getElementById(‘number’).style.color=”red”;
result = false;
}
if (document.Entry.number.value==””) {
msg+=”You must enter the number \n”;
document.Entry.number.focus();
document.getElementById(‘number’).style.color=”red”;
result = false;
}
change this to
if (document.Entry.number.length != 4){
msg+="Number must be exactly 4 characters \n";
document.Entry.number.focus();
document.getElementById('number').style.color="red";
result = false;
}
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;
I am trying to validate a html form using a javascript function, but the function is never matching the values to regex even when they are correct. This is what I have:
function validateForms(form){
var usZipRegex = '^\d{5}[[$-.+ ]?\d{4}]?$|^\d{5}$';
var canZipRegex = '^[ABCEGHJKLMNPRSTVWXY][\d][ABCEGHJKLMNPRSTVWXYZ][\W]?[\d][ABCEGHJKLMNPRSTVWXYZ][\d]$';
var nameRegex = '^[A-Za-z][ -a-zA-Z]+$';
var emailRegex = '^[^\.#]([\w]*\.)*[^\.#]*#[a-zA-Z0-9-]{1,63}(\.[a-zA-Z0-9-]{1,63})+$';
var name = form.name.value;
var zip = form.zip.value;
var email = form.email.value;
var comment = form.comment.value
if(name == "") {
alert('You must enter your name.');
return false;
}
if(name != nameRegex) {
alert('You have entered an invalid name.');
return false;
}
if(email == "") {
alert('You must enter your email.');
return false;
}
if(email != emailRegex) {
alert('You have entered an invalid email.');
return false;
}
if(zip == "") {
alert('You must enter your zip code.');
return false;
}
if((zip != usZipRegex) & (zip != canZipRegex)) {
alert('You have entered an invalid zip code.');
return false;
}
if(comment == "") {
alert('You must enter a message.');
return false;
}
return true;
}
The (name != nameRegex) parts always return true, but should not. The (name == "") parts do work on their own though.
You're comparing the raw strings, not running the regex. name != nameRegex will always return true unless the person's name is literally ^[A-Za-z][ -a-zA-Z]+$. You need to set up nameRegex properly as a regex object, but in your code, it's just a string. You also need to use test or another one of the RegExp object's methods to check for a match, not the ==/!= operators. This is true for the other patterns as well.
Constructing a regex is done one of two ways:
var regex1 = /[a-z0-9]/;
var regex2 = new RegExp('[a-z0-9]');
If you do it like this, though, it's just a string:
var notARegex = '[a-z0-9]';
Your code should be closer to this style:
var nameRegex = /^[A-Za-z][-a-zA-Z ]+$/; //note that the hyphen has been moved
//...
if (!nameRegex.test(name)) {
alert('You have entered an invalid name.');
return false;
}
Edit: There's more going wrong here. Your other regex code could use some major improvements:
var usZipRegex = /^\d{5}([$.+ -]?\d{4})?$/;
var canZipRegex = /^(?![DFIOQU])[A-Y]\d(?![DFIOQU])[A-Z]\W?\d(?![DFIOQU])[A-Z]\d$/;
var nameRegex = /^[a-z][a-z\s-]+$/i;
var emailRegex = /^[^.#]+#[^.#]{1,63}(\.[^.#]{1,63})+$/i';
I don't know if your Canadian zip code pattern is correct, but the email one was definitely wrong (email is WAY more complicated than you think, so it's best to be very permissive with it).