I've learned to make the login form and I planning to validate two values ( email and password ) and use it as a condition true or false .
i have code like this
function userValid(email) {
var usr = /^([\w-]+(?:\.[\w-]+)*)#((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$\i/ ;
return usr.test(email);
};
function passValid(password) {
var passw = /^[A-Za-z]\w{7,14}$/;
return passw.test(password);
};
the code above it use to validating email and password in my form and i think if i passing a value to that function (for example passing value email to userValid() function) it will return true/false.
i plan to create validating form or login form that first check the user email & password to be valid before i submit (lets say to the server).
and i think the logic is like this :
if both values is TRUE then Login Success
if one of the values is TRUE then Login Failed
if both values is FALSE then Login Failed
else (the value must be EMPTY or NULL) then Please Insert Email and Password
and so on
my question is how to create conditional statement with ABOVE case, and of course in JAVASCRIPT :) thanks.
I don't know how you acces the email and the password, but this should work if you fill those in.
if ( userValid(email) == true && passValid(password) == true ) {
//Login succes code
}
else if ( userValid(email) == false || passValid(password) == false ){
//Login failure code
}
else {
//The "Please insert" code
}
The && operater (and) returns true only if both values are true.
The || operater (or) returns true if at least one of the values is true.
This can probably be done easier, but it should work like this.
You can add code to check if the password and the username are empty, but I don't think that's necesarry.
So what you want to do is create a function to be called when the user tries to submit your form. If this function returns false, the form will not submit. Otherwise it submits the form. You could write it like this:
var yourForm = document.getElementById("yourForm");
var yourEmail = document.getElementById("yourEmail");
var yourPassword = document.getElementById("yourPassword");
var yourWarningLabel = document.getElementById("yourWarningLabel");
yourForm.onsubmit = function() {
if (yourEmail.value == "" || yourPassword.value == "") { //Check if the fields have something in them
yourWarningLabel.innerHTML = "Please Insert Email and Password";
return false;
} else return (userValid(yourEmail.value) && passValid(yourPassword.value));
};
Where yourWarningLabel is a <p>, <label> or other text tag that you use to display the warning.
The double pipes || represent an OR operator, meaning that if the email is empty OR the password is empty we should display a warning.
The double ampersands && represent an AND operator, meaning if the email passes AND the password passes the check then we return true and submit the form, otherwise it returns false.
In addition to this you should also be checking and cleaning input on your server side or in the page that uses the input.
Related
I have 2 fields in my form say carriercode and billnum, here I need to validate number should always have value of carriercode as a prefix, ex if carriercode=ABCD blnum should be ABCD followed by any charter can be string or number.
return Yup.object({
carriercode: Yup.string().required(requiredMessage).min(4,"length should be 4").matches(/^[a-zA-Z0-9-]+$/,"Can not contain special characters like ),(,# etc."),
blnum: Yup.string().required(requiredMessage) //validate if blnum starts with carriercode
})
}
is there any way to achieve this using yup validations, to be simple I need something like startsWith/indexOf functionality in yup.
The test method can be one of those. Inside test method, you can access other fields(here carriercode) by using this.parent['carriercode']. Just make a custom validator like this:
Yup.object({
carriercode: Yup.string().required(requiredMessage).min(4, "length should be 4").matches(/^[a-zA-Z0-9-]+$/, "Can not contain special characters like ),(,# etc."),
blnum: Yup.string()
.required("Should be the prefix of carriercode") //validate if blnum starts with carriercode
.test("Check prefix", function () {
let carriercode = this.parent["carriercode"];
let blnum = this.parent["blnum"];
// console.log(carriercode, blnum);
if (carriercode && blnum) {
return blnum.startsWith(carriercode) ? true : false;
}
})
})
Hello, There are simple problem in my code, I've put myself as a user, let's assume that if the user click on the space button (in keyboard), So What is the solution.
Here my simple code:
var name = $('input#name').val(); // get the value of the input field
if(name == "" || name == " ") {
$('#err-name').fadeIn('slow'); // show the error message
error = true; // change the error state to true
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Use the $.trim function to remove spaces
var name = $.trim( $('input#name').val() ); // get the value of the input field
if(name == "") {
$('#err-name').fadeIn('slow'); // show the error message
error = true; // change the error state to true
}
The .trim function in JavaScript removes leading and trailing spaces/new lines. So, if the user just spams space bar, the name.trim() will remove all leading/trailing spaces, resulting in "" and that equals "". Thus, your error code would show.
var name = $('input#name').val(); // get the value of the input field
if(name.trim() == "") {
$('#err-name').fadeIn('slow'); // show the error message
error = true; // change the error state to true
}
Im trying to implement a validation for an input field in IBM BPM.
Im not really familiar with java script but I try to get method that returns
ture if a string contains any numbers.
awdadw = valid
awdawd2d = invalid
I tried this method:
function hasNumbers(t)
{
var pattern=new RegExp("^[A-Za-z]+$");
return pattern.test(t); // true if string. Returns false for numbers
}
When I try this function, it says that method / variable RegExp is unknown. Since it is rather basci stuff I hope to get some sources where this topic is explained.
You can use this:
function validate(){
var re = /^[A-Za-z]+$/;
if(re.test(document.getElementById("inputID").value))
alert('Valid Name.');
else
alert('Invalid Name.');
}
Based on adre3wap this worked for me:
function validate(t){
var re = /^[A-Za-z]+$/;
if(re.test(t))
return false;
else
return true;
}
I had created an registration form in which i had created and email i had provided validation to all the filed but I'm confused how to give validation to email field because i want that email should be either gmail.com or yahoomail.com if some one enters any other email even yahoomail.co.in it should give error message.
here is the code i which checking that its having # and . in the email or not
var atdrate=email.indexOf("#");
var dot1=email.indexOf(".");
else if(atdrate<1 || dot1<1)
{
alert("Enter valid Email");
if(gml<atdrate+1)
alert("Enter a vaild mail id");
else if(yml<atdrate+1)
alert("Enter a valid email id");
}
Using Regular Expressions is probably the best way. Here's an example (live demo):
function validateEmail(email) {
var re = /^(([^<>()[\]\\.,;:\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,}))$/;
return re.test(email);
}
But keep in mind that one should not rely only upon JavaScript validation. JavaScript can easily be disabled. This should be validated on the server side as well.
*answer from https://stackoverflow.com/a/46181/1521984
Update: Then use the JavaScript split() function to split the mail address after the '#' and check the value versus your strings.
var mail = yourMailAdress.split("#");
if (mail[1] == "gmail.com" || mail[1] == "yahoomail.com") {
// OKAY
} else {
// false
}
This is part of my jQuery script. I need to make the system validate emails for a specific domain.
like example#schooldomain.com
And only allow emails from #schooldomain.com
Code:
email: function(value,element){return this.optional(element)||/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))#((([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])))\.?$/i.test(value);}
Firstly, as pointed out in the comments, validate the email using regex, and then check if the email is from the right domain.
function validateEmail(email) {
var re = /^(([^<>()[\]\\.,;:\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,}))$/;
if(re.test(email)){
//Email valid. Procees to test if it's from the right domain (Second argument is to check that the string ENDS with this domain, and that it doesn't just contain it)
if(email.indexOf("#thedomain.com", email.length - "#thedomain.com".length) !== -1){
//VALID
console.log("VALID");
}
}
}
Thanks to this thread I found another solution for only accepting one specific domain after the "at" / "#". Get everything after the dash in a string in JavaScript
Basically dividing the email in two, the text before # and the text after #. If the text after # is not equal to the specified domain the validation will be false.
// Email validation
let em = document.forms['Login']['email'].value;
let atpos = em.indexOf("#");
let domain = em.split("#")[1]; // Saves user input after the # (at)
if (em == null || em == "") {
alert("Email can not be empty.");
document.getElementById('e').focus();
return false;
}
// First test checks for atleast one character before #
else if (atpos < 1 || domain != "gmail.com"){ // Second test checks if the user entered a gmail.com domain after #
alert("Not a valid e-mail address. Please write your gmail address like this: username#gmail.com.");
document.getElementById('e').focus();
return false;
}