Home > front end >  How can I validate email,firstname and lastname?
How can I validate email,firstname and lastname?

Time:10-26

I try to validate firstname,lastname and email taken by user and return true if its valid or return false if it is not valid then ask the user to enter again as long as it is not valid.By writing a function in Javascript. For names just string,hyphen and whitespace and for email just string,htphen,underscore,@ and dot is valid.

1-How can i get rid of "acceptable" and just make it as return true/false? 2-How can I modify the code to validate both the email and first,lastname because when i write @ for example in firstname it accepts.

function validateText(text, validChares = "abcdefghijklmnopqrstuvwxyz -") {
  let acceptable;
  for (let t of text) {

    acceptable = false;
    for (let vc of validChares) {
      if (t === vc) {
        acceptable = true;
        break;
      }

    }
    if (!acceptable)
      return false;
  }
  return true;
}

let validChars = "";
for (let i = 65; i <= 90; i  )
  validChars  = String.fromCharCode(i);
for (let i = 97; i <= 122; i  )
  validChars  = String.fromCharCode(i);
validChars  = " @_-.";

//return validChars  = " -";
let firstName = prompt("Enter your firstname");
if (validateText(firstName, validChars))
  alert(`${firstName} is acceptable`);
else
  alert(`${firstName} is not acceptable`);

while (!validateText(firstName)) {
  firstName = prompt("Enter valid First Name:");
}

/**/
let lastName = prompt("Enter your lastname");
if (validateText(lastName, validChars))
  alert(`${lastName} is acceptable`);
else
  alert(`${lastName} is not acceptable`);

while (!validateText(lastName)) {
  lastName = prompt("Enter valid Last Name:");
}

/**/
let email = prompt("Enter your email");
if (validateText(email, validChars))
  alert(`${email} is acceptable`);
else
  alert(`${email} is not acceptable`);
while (!validateText(email)) {
  email = prompt("Enter valid Email:");
}

alert(`Registration data:\nName: ${firstName}\nSurname: ${lastName}\nEmail: ${email}`)

CodePudding user response:

You can instead use regex to test your name and email

for name,

const onValidName = (val) => {
    // name can contain 
    // CAPITAL ALPHABETS
    // small alphabets
    // whitespace
    // hyphen
    const nameRegex = /^[a-zA-Z- ]*$/
    return nameRegex.test(val)
  }

//it will log true if name is valid
console.log(onValidName(somename)  

for email validation, you can use

const onValidEmail = (val) => {
    // cheks for email is valid or not
    const emailRegex = /^(([^<>()[\]\\.,;:\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 emailRegex.test(val)
  }

//return true if email is valid
console.log(onValidEmail(some email))

you can keep asking user for a valid name/email by this way

let lastName = prompt("Enter your lastname");
if (onValidName(lastName))
  alert(`${lastName} is acceptable`); //update UI or write in DB or do whatever you want
else{
  alert(`${lastName} is not acceptable`); //or you can show error in UI
  prompt("Enter your lastname"); //again ask for a valid name
}

rest all validations for [name, email, etc] can be done in the same way

  • Related