Email Validation using setCustomValidity - javascript

I got the following code for
// Validating Email
function validateEmail() {
var mail = document.getElementById("email").value;
var mailformat = /^\w+([\.-]?\w+)*#\w+([\.-]?\w+)*(\.\w{3})+$/;
var endEmail = "#gmail"
if(mail.match(mailformat) && (mail.charAt(x.length-1) == endEmail) ) {
mail.value += ".com"
alert(mail)
return true;
} else if (email.validity.valueMissing) {
console.log("Email is missing")
email.setCustomValidity("Please fill in the email.");
}
}
I have a problem with checking the email. The logic I am trying to implement is to check whether the email contains “gmail” after “#” and to validate the email ends in “.com” (This check is only mandatory if the email field has “gmail” after “#”.). In the case of gmail being in the email after “#” and the ending being something other than “.com”, I wanna fix this for the user and let the submission go through after the fix.

Related

How to validate input for only email friendly characters?

I have JavaScript that validates whole email once entered:
function ValidateEmail(inputText)
{
var mailformat = /^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+#[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/
if(inputText.value.match(mailformat))
{
alert("This is not a valid email address");
return false;
}
}
But how to check if single character is valid for email?
So I want to prevent users to enter bad email characters.
So function param would be these and should return false only if invalid character present:
m
my
mya
....
myaddress
....
myaddress#mail.com
great answer:
https://stackoverflow.com/a/46181/3764369
issues follows RFC 5322
https://emailregex.com/
w3 code example:
function ValidateEmail(inputText)
{
var mailformat = /^\w+([\.-]?\w+)*#\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if(inputText.value.match(mailformat))
{
alert("Valid email address!");
document.form.email.focus();
return true;
}
else
{
alert("You have entered an invalid email address!");
document.form.email.focus();
return false;
}
}

Dynamically change div content in a form submission

I have created one login page with some error message.But I am able to display only single error message.My Requirement is very simple.
I have 2 fields email and password when I click go button if there is no email it should display enter email.If I have entered invalid email it should display invalid email.
Similary for password two validation enter passwordand enter valid password.
For this I need to add error messages dynamically.Can anybody help me ?please.
https://jsfiddle.net/jnzk9gv4/
The fastest way here is to simply change the message when you detect a problem.
here you have: https://jsfiddle.net/jnzk9gv4/7/
var error = "";
if (!emailReg.test(userEmail)) {
error = "Invalid email";
} else if (userEmail=="") {
error = "Enter email";
} else if (!passReg.test(userPassword)) {
error = "Invalid password";
} else if (userPassword=="") {
error = "Enter password";
}
if (error != "") {
$("#errorMessage").text(error);
$(".invalidData").show();
} else {
$(".invalidData").hide();
}

validating reCaptcha in javascript function that get called on submit. (ASP classic)

I need help in validating the response of ReCaptcha in javascript validation which is made for other validations like, n Field is empty etc..
The javascript function function verify(f) {....} get called on onSubmit="return verify(this);" in html <form name="form2" method="POST" action="alink.asp" onSubmit="return verify(this);">
Bellow is the complete js function:
function verify(f) {
var msg = '';
var s = f.CKRoutingNumber.value;
s = s.replace(/[^0-9]/gi, "");
f.CKRoutingNumber.value = s;
if (f.CustomerID.value == '') { msg = 'Please enter your Bricks R Us Customer ID.'; f.CustomerID.focus(); }
else if (f.PurchaseOrderNumber.value == '') { msg = 'Please enter the purchase order number.'; f.PurchaseOrderNumber.focus(); }
else if (f.Amount.value == '') { msg = 'Please enter the amount you wish to pay.'; f.Amount.focus(); }
else if (f.CKBankName.value == '') { msg = 'Please enter a value into the Bank Name field.'; f.CKBankName.focus(); }
else if (f.CKRoutingNumber.value == '') { msg = 'Please enter a value into the Routing Number field.'; f.CKRoutingNumber.focus(); }
else if (s.length != 9) { msg = 'Please enter a valid nine-digit routing/transit number.'; f.CKRoutingNumber.focus(); }
else if (f.CKAccountNumber.value == '') { msg = 'Please enter a value into the Account Number field.'; f.CKAccountNumber.focus(); }
else if (f.CKNumber.value == '') { msg = 'Please enter a value into the Check Number field.'; f.CKNumber.focus(); }
else if (f.BillingName.value == '') { msg = 'Please enter a value into the Full Name field.'; f.BillingName.focus(); }
else if (f.BillingAddress.value == '') { msg = 'Please enter a value into the Billing Address field.'; f.BillingAddress.focus(); }
else if (f.BillingCity.value == '') { msg = 'Please enter a value into the Billing City field.'; f.BillingCity.focus(); }
else if (f.BillingState.value == '') { msg = 'Please select a value for the Billing State field.'; f.BillingState.focus(); }
else if (f.BillingZIPCode.value == '') { msg = 'Please enter a value into the Billing ZIP Code field.'; f.BillingZIPCode.focus(); }
else if (f.BillingPhone.value == '') { msg = 'Please enter a value into the Phone Number field.'; f.BillingPhone.focus(); }
if (msg != '') {
alert(msg);
return false;
}
}
The above function is on the same page in which the form is made.
Bellow is the ASP classic code which get response from reCaptcha. Its also on the same page
<%
Dim reresponse
reresponse= Request.form("g-recaptcha-response")
Dim VarString
VarString = _
"?secret=6Lex3CMTAAAAAASVS5XnIq4Ya5ZGvEH_W70NU&" & _
"&response=" & reresponse & _
"&&remoteip=" & Request.ServerVariables("REMOTE_ADDR")
Dim url
url="https://www.google.com/recaptcha/api/siteverify" & VarString
Dim objXmlHttp
Set objXmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP")
objXmlHttp.open "POST", url, False
objXmlHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objXmlHttp.send
Dim ResponseString
ResponseString = objXmlHttp.responseText
Set objXmlHttp = Nothing
If instr(ResponseString, "success" & chr(34) &": true")>0 then
// do nothing
else
// Here I want to get this response message and validate it in the above javascript function.
end if
%>
I'm confused that how can I get the response from asp and validate it in the verify(f) javascript function so that I also get alert message on submit button that the recaptcha is required and or incorrect.
My intention is to validate the reCaptcha response in same veryify javascript function which get called on submit and shows validation in alert()
Remember, both asp code and javascript code are in the same page.
Please ask if you also need my form html code
Your verify() function is running locally and doing some input value checking/alerting is OK, but in any case you should check whatever comes from the browser on de server side. If you would send the ReCaptscha response back to that verify() function you undermine your security because your users could simple change that verify() function ...

Javascript Validation

I am wanting to make validation to my Email form. Unfortunately I cannot get this piece of code to work. When I am clicking submit without entering any data in to the form it will send.
I am wanting it not to send without the right Characters in the Email
I have attached a screenshot below of my code
You need to change your regex as:
var email_regex = /^[\w%_\-.\d]+#[\w.\-]+.[A-Za-z]{2,6}$/; // reg ex email check
if(emailAddress == "")
{
// error if empty
}
else
{
if(!email_regex.test(emailAddress))
{
// invalid error
}
else
{
// success
}
}

Form submit add domain to username field if missing

I have a login form that includes a username and password field.
Users will be able to login using:
Domain\username
And
Username#domain.org.uk
However many users attempt to login using just 'username'
I want to help users by adding domain\ or #domain.org.uk to there username when they enter just 'username', when they click the login button I want to add the domain part of the username.
How can I do this in pure JavaScript?
function insertDomain (){
var txtBox = document.getElementById('Your_Textbox');
if(txtBox.value.indexOf("#") == -1)
{
txtBox.value += "#domain.org.uk";
}
}
On Submit: http://alexking.org/blog/2003/10/01/javascript-onsubmit-handler
Something along the lines of
var username = document.getElementById('username')
if(username.indexOf('#') < 0){
username = usename + '#domain.org.uk';
}

Categories