mardi 13 août 2019

Properly connecting a regex with if statements for inputs

Directions:Within your HTML file, add the following:

Create a form that asks the user to input their first and last name into two different inputs

Your HTML page should include instructions based on your function. For example:

<h1>Please Enter Your First and Last Name</h1>

Within your JavaScript file, add the following:

Write a function named regexChecker() that does the following:
    Pulls in the users information using the DOM from the HTML form
    Checks the following against the user's form information using Regex:The first letter of the first name should be capitalized
  The first letter of the last name should be capitalized
   The user must enter more than one character for first and lastname.
    No special characters should be used.If all of these requirements are matched, alert the string "Yay! Your inputs were all correct!"        If all of the requirements are not matched, alert the string "Oh no! Thats an invalid format!"

HTML:

<!DOCTYPE html>
<html>
    <head>

    </head>
    <script src="script.js"></script>
    <body>
      <form>
        <h1>Please Enter Your First and Last Name</h1>
        First Name:<input type="text" name="firstname">
        Last Name:<input type="text" name="lastname">
        <input type="submit">
      </form>
    </body>
</html>

JS:

function regexChecker(firstname,lastname){
let firstname = /^[A-Z][A-Za-z]+$/
let lastname = /^[A-Z][A-Za-z]+$/
if (regexChecker.test(firstname&&lastname)){
    alert("Yay! Your inputs were all correct!");
} else {
    alert("Oh no! Thats an invalid format!");
}
}

When attempted nothing appears with alert boxes

Aucun commentaire:

Enregistrer un commentaire