Before submitting the form I need a function to check for # and . symbols.
Function responsible for checking that values are inserted:
// function ValidateAndSend
function ValidateAndSend (event:MouseEvent):void {
// validate fields
if(!name_txt.length) {
status_txt.text = "Please enter your name";
} else if (!email_txt.length) {
status_txt.text = "Please enter your e-mail address";
} else if (!phone_txt.length) {
status_txt.text = "Please enter your phone number";
} else {
// All is good, send the data now to PHP
// ready the variables in our form for sending
variables.userName = name_txt.text;
variables.userEmail = email_txt.text;
variables.userPhone = phone_txt.text;
variables.userShop = shopList.value;
// Send the data to PHP now
varLoader.load(varSend);
} // close else condition for error handling
} // close validate and send function
I've tried creating a separate function for checking the e-mail symbols:
// Checking e-mail
function checkEmail():Boolean {
var reEmail:String = email_txt.text;
var emailOk:Boolean = false;
var checkArray1:Array = reEmail.split("#");
if (checkArray1.length >1) {
var checkArray2:Array = checkArray1[1].split(".");
if (checkArray2.length >1) {
emailOk = true;
}
}
return emailOk;
}
but this doesn't work. How would you achieve this?
Update: I've tried running the function inside ValidateAndSend function. But now if the email address is wrong it won't send the message but it still displays a successful submitting message.
// function ValidateAndSend
function ValidateAndSend (event:MouseEvent):void {
// validate fields
if(!name_txt.length) {
status_txt.text = "Please enter your name";
} else if (!email_txt.length) {
status_txt.text = "Please enter your e-mail";
// Checking e-mail
function checkEmail():Boolean {
var reEmail:String = email_txt.text;
var emailOk:Boolean = false;
var checkArray1:Array = reEmail.split("#");
if (checkArray1.length >1) {
status_txt.text = "Please check your e-mail address";
var checkArray2:Array = checkArray1[1].split(".");
if (checkArray2.length >1) {
emailOk = true;
}
}
return emailOk;
}
} else if (!phone_txt.length) {
status_txt.text = "Please enter your phone number";
} else {
// All is good, send the data now to PHP
// ready the variables in our form for sending
variables.userName = name_txt.text;
variables.userEmail = email_txt.text;
variables.userPhone = phone_txt.text;
variables.userShop = shopList.value;
// Send the data to PHP now
varLoader.load(varSend);
} // close else condition for error handling
} // close validate and send function
You should use regular expressions for this.
Your checkEmail() function won't be needed
// function ValidateAndSend
function ValidateAndSend (event:MouseEvent):void {
var emailCheckRegExp:RegExp = /^[\w.-]+#\w[\w.-]+\.[\w.-]*[a-z][a-z]$/i;
// validate fields
if(name_txt.length == 0) {
status_txt.text = "Please enter your name";
}
else if (email_txt.length == 0) {
status_txt.text = "Please enter your e-mail address";
}
else if (phone_txt.length == 0) {
status_txt.text = "Please enter your phone number";
}
else if(emailCheckRegExp.exec(email_txt.text) == null)
{
status_txt.text = "Entered e-mail is not valid";
}
else {
// All is good, send the data now to PHP
// ready the variables in our form for sending
variables.userName = name_txt.text;
variables.userEmail = email_txt.text;
variables.userPhone = phone_txt.text;
variables.userShop = shopList.value;
// Send the data to PHP now
varLoader.load(varSend);
} // close else condition for error handling
} // close validate and send function
Related
Making a JS/HTML validation form for my website. The form works great but I want to show an alert dialog box showing the users inputs before submitting successfully. I initialize var dataPreview to return if no errors occur, however upon pressing submit, it goes straight to the confirmation page. Any feedback is appreciated!
function validateForm(){
var name = document.contactForm.name.value;
var email = document.contactForm.email.value;
var mobile = document.contactForm.mobile.value;
var address = document.contactForm.address.value;
var birth = document.contactForm.birth.value;
var nameErr = emailErr = mobileErr = addressErr = birthErr = true;
if(name == "") {
printError("nameErr", "Please enter your name");
} else {
var regex = /^[a-zA-Z\s]+$/;
if(regex.test(name) === false) {
printError("nameErr", "Please enter a valid name");
} else {
printError("nameErr", "");
nameErr = false;
}
}
if(email == "") {
printError("emailErr", "Please enter your email address");
} else {
// Regular expression for basic email validation
var regex = /^\S+#\S+\.\S+$/;
if(regex.test(email) === false) {
printError("emailErr", "Please enter a valid email address");
} else{
printError("emailErr", "");
emailErr = false;
}
}
if(mobile == "") {
printError("mobileErr", "Please enter your mobile number");
} else {
var regex = /^[1-9]\d{9}$/;
if(regex.test(mobile) === false) {
printError("mobileErr", "Please enter a valid 10 digit mobile number");
} else{
printError("mobileErr", "");
mobileErr = false;
}
}
if(address == "") {
printError("addressErr", "Please enter your address");
} else {
var regex = /^[a-zA-Z\s]+$/;
if(regex.test(address) === false) {
printError("addresasErr", "Please enter a valid name");
} else {
printError("addressErr", "");
addressErr = false;
}
}
if(birth == "") {
printError("birthErr", "Please enter your birthday");
} else {
var regex = /^[a-zA-Z\s]+$/;
if(regex.test(birth) === false) {
printError("birthErr", "Please enter a valid birthday");
} else {
printError("birthErr", "");
birthErr = false;
}
}
***if((nameErr || emailErr || mobileErr || addressErr || birthErr) == true){
return false;
} else{
var dataPreview = "You have entered the following information: \n" +
"Full Name: " + name + "\n" +
"Email Address: " + email + "\n" +
"Mobile Number: " + mobile + "\n" +
"Address: " + address + "\n" +
"Birthday: " + birth + "\n";
alert(dataPreview);
}
}
</script>
Hello everyone)) I have problem with moving value inside th function, please help me
First I have field "allValid" - has taken boolean value "true".
Next I did 3 checking breakpoints in script where this field must change boolean value on "false".
But whatever i do allValid always has "true". I cant understand what am I doing wrong.
function ButtonClickAction(){
let allValid = true;
var UserInfo = {};
UserInfo.zLOGIN = document.getElementById("LOGIN").value;
UserInfo.zSSCC = document.getElementById("SSCC").value;
UserInfo.zPLACE = document.getElementById("PLACE").value;
var toValidate = {
LOGIN: "LOGIN REQUIRED",
SSCC: "SSCC REQUIRED",
PLACE: "PLACE REQUIRED"
}
var idKeys = Object.keys(toValidate);
//first checking
idKeys.forEach(function(id){
isValid = checkIfValid(id,toValidate[id]);
allValid = isValid;
});
//second checking
google.script.run.withSuccessHandler(onLOGIN).searchLogins(UserInfo);
function onLOGIN(findLogin) {
if (!findLogin) {
alert("LOGIN NOT EXIST");
allValid = false;
}
}
//thirdchecking
console.log(allValid);
google.script.run.withSuccessHandler(onSSCC).searchSSCC(UserInfo);
function onSSCC(findSSCC) {
if (!findSSCC) {
alert("SSCC NOT EXIST");
allValid = false;
}
}
//finish result
if (!allValid){
alert("YOU HAVE NOTHING");
}
else {
addRecord(idElem);
}
}
function checkIfValid(elId, message){
var isValid = document.getElementById(elId).checkValidity();
if(!isValid){
alert(message);
return false;
}
return true;
}
At the end of the function "ButtonClickAction" I have "finish result", but it works wrong because allValid has always "true". Help!
(sorry for bad English, it's my non-native language, I try explain correctly)
... and some server code:
var url = "https://docs.google.com/spreadsheets/d/1s8l-8N8dI-GGJi_mmYs2f_88VBcnzWfv3YHgk1HvIh0/edit?usp=sharing";
var sprSRCH = SpreadsheetApp.openByUrl(url);
let sheetSRCHSSCC = sprSRCH.getSheetByName("PUTAWAY_TO");
let sheetTSD = sprSRCH.getSheetByName("ПРИВЯЗКА МЕСТ");
function searchLogins(UserInfo){
let sheetSRCHLGN = sprSRCH.getSheetByName("ЛОГИНЫ");
let findingRLGN = sheetSRCHLGN.getRange("A:A").getValues();
let valToFind = UserInfo.zLOGIN;
for (let i = 0; i < findingRLGN.length; i++){
if(findingRLGN[i].indexOf(valToFind)!==-1){
return true;
}
};
return false;
}
function searchSSCC(UserInfo){
let findingRSSCC = sheetSRCHSSCC.getRange("M:M").getValues();
let valToFind = UserInfo.zSSCС;
for (let i = 0; i < findingRSSCC.length; i++){
if(findingRSSCC[i].indexOf(valToFind)!==-1){
return true;
break;
}
};
indx=2;
return false;
}
I'm trying to validate the value in my name element and email address element.
but the below code is not providing the proper validation and my code is still jumping to my php file with wrongs inputs in it.
can anybody please provide me some guidance on this?
var letters = /^[A-Za-z]+$/;
var mailformat = ^\w+([\.-]?\w+)*#\w+([\.-]?\w+)*(\.\w{2,3})+$;
if( document.addform.First_name.value.match(letters)) {
return true;
}
else {
alert( "Username must have alphabet characters only" );
document.addform.First_name.focus() ;
return false;
}
if( document.addform.email.value.match(mailformat) ) {
return true;
}
else {
alert( "You have entered an invalid email address!" );
document.addform.email.focus() ;
return false;
}
You need to return true if both conditions are met, not just one of them.
function validateFormInput () {
let yourUsernameRegEx = new RegExp(...);
let yourMailRegEx = new RegExp(...);
if (!document.addform.First_name.value.match(yourUsernameRegEx)) {
// Other code here (alerts, popups, etc.) ...
return false;
}
if (!document.addform.email.value.match(yourMailRegEx)) {
// Other code here (alerts, popups, etc.) ...
return false;
}
return true;
}
This is the controller file of my app. It adds the data perfectly, but when it reaches the then key word google chrome hangs but key parameter is also added to the data base. I can't figure out where the problem is.
.controller('recordsCtrl', ['$scope','$firebaseArray',function($scope,$firebaseArray) {
$scope.records = $firebaseArray(rootRef);
//show form
$scope.showAddForm = function(){
$scope.addFormShow = true;
}
// hide form
$scope.hide = function(){
$scope.addFormShow = false;
}
// submit contact
$scope.addFormSubmit = function() {
console.log("adding form...")
// Assign values
if ($scope.lname) { var lname = $scope.lname; } else { var lname = null; }
if ($scope.mname) { var mname = $scope.mname; } else { var mname = null; }
if ($scope.fname) { var fname = $scope.fname; } else { var fname = null; }
if ($scope.email) { var email = $scope.email; } else { var email = null; } if ($scope.conId) { var conId = $scope.conId; } else { var conId = null;}
// Build Object
$scope.records.$add({
fname: fname,
lname: lname,
mname: mname,
email: email,
company: company,
conId: conId
}).then(function(rootRef) {
***//this is not printed in the console but the key is assigned to the database***
console.log("Assign root key");
var id = rootRef.key();
console.log("Added Record with ID: " + id);
// clear Form
clearFields();
// Hide Form
$scope.addFormShow = false;
// send message to use
$scope.msg = "Record Added";
});
}
}]);
My team ran into this issue when we updated to Firebase 3.7.x. I believe this is a known bug.
Try downgrading to Firebase 3.6.x.
function validEmail (emailAddress)
{
for (var index=0; index < emailAddress.length; index++)
{
if ( emailAddress[index] == "#")
{
for (var count=0; count <emailAddress.length; count++)
{
if (emailAddress[count] == ".")
{
return true;
}
}
}
else
{
return false;
}
}
}
function btnParseAddress_onclick()
{
var emailAddress = addressTextbox.value;
var userName = emailUsername(emailAddress);
var domain = emailDomain (emailAddress);
var valid = validEmail (emailAddress);
if (valid)
{
outputTextbox.value = "Username:" + userName + "\nDomain:" + domain
}
else
{
outputTextbox.value = "Invalid Email Address"
}
}
Now the point of this assignment is to return a username and a domain from an inputted email address. I have deleted other functions and variables to help focus better on the problem.
I need to validate the email address first. I need to make sure there is an "#" and a "." within the string entered and return the value with true or false. True having the strings and false not having the strings. When I run the file, the return value is always false. I can't figure out if it is my for loop in the validEmail function or the if statement in btnParseAddress_onclick function
You can avoid the for loop by using IndexOf twice:
if (emailAddress.IndexOf('.', emailAddress.IndexOf('#')+1) > 0) {
Console.WriteLine("The e-mail address looks valid.");
}