Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //index.html
- <!DOCTYPE html>
- <html>
- <head>
- <title>Client Side Validation</title>
- <link rel="stylesheet" type="text/css" href="main.css" />
- </head>
- <body>
- <div class="container">
- <h3>I am trying to validate this form on the client</h3>
- <br>
- <form
- id="reg_form"
- name="reg_form"
- method="post"
- action="register.php"
- onsubmit="return validate();"
- >
- <fieldset>
- <legend>Registration</legend>
- First Name: <input type="text" name="fname" id="fname" />
- <span id="fname_err" class="error"></span>
- Last Name: <input type="text" name="lname" id="lname" /><br /><br />
- <span id="lname_err" class="error"></span>
- Email: <input type="email" name="email" id="email" />
- Birth Year:
- <input type="number" name="birth_yr" id="birth_yr" /><br /><br />
- Gender:
- Male <input type="radio" name="gender" id="male" value="male" />
- Female <input type="radio" name="gender" id="female" value="female" />
- <br /><br />
- <input type="submit" name="submit" value="Register" />
- </fieldset>
- </form>
- </div>
- <script type="text/javascript" src="myJS.js"></script>
- <!--From js notes-->
- <script type="text/javascript">
- Document.write("Hello World");
- </script>
- <p id="learningJS">
- </p>
- <script type="text/javascript">
- document.getElementById('learningJS').innerHTML="<br> <strong>This is an example of using the getElementById and innerHTML functions in JavaScript </strong> <br><br> Thank you!";
- </script>
- </body>
- </html>
- //register.php
- <!DOCTYPE html>
- <html>
- <head>
- <title>Client Side Validation</title>
- <link rel="stylesheet" type="text/css" href="main.css">
- </head>
- <body>
- <div class="container">
- <h3>Registered!</h3>
- <br />
- <p>Hello <?php echo "$_POST[fname]. Thank you for registering with us."; ?></p>
- </div>
- <script type="text/javascript" src="myJS.js"></script>
- </body>
- </html>
- //myJS.js
- function validate(){
- // clearErrors();
- //declare variables in js with the keyword let
- let valid = true;
- // validate the first name data
- let fname_element = document.getElementById('fname');
- // retrieves the input object with ID fname.
- let fname_regex = /^[a-zA-Z'!-]{2,30}$/;
- //matches all letters, apostrophes, exclamation, and hyphen characters
- if (!fname_regex.test(fname_element.value)){
- fname_element.value = "";
- fname_element.focus();
- fname_element.style.backgroundColor = "#ff5555";
- valid = false;
- }
- // // An alternative approach is:
- // if(!fname_regex.test(fname_element.value)){
- // // write a message directly onto the web page using the innerHTML property
- // var fn_err = document.getElementById('fname_err');
- // fn_err.innerHTML = "Only letters, !,' and - allowed.";
- // valid = false;
- // }
- //validate last name data
- let lname_element = document.getElementById('lname');
- let lname_regex = /^[a-zA-Z!'-]{2,40}$/;
- if(!lname_regex.test(lname_element.value)){
- lname_element.value="";
- lname_element.focus();
- lname_element.style.background ="#ff5555"
- valid= false;
- }
- //validate email data
- let email_element = document.getElementById('email');
- let email_regex = /^[a-zA-Z0-9.]{2,30}@[a-zA-Z0-9.]{2,20}.[a-zA-Z]{2,4}$/;
- if(!email_regex.test(email_element.value)) {
- email_element.value = "";
- email_element.focus();
- email_element.style.background = "#ff5555";
- valid = false;
- }
- //birth year validation
- let birth_yr_element = document.getElementById('birth_yr');
- let birth_yr_regex = /^[0-9]{4}$/;
- if(!birth_yr_regex.test(birth_yr_element.value)){
- birth_yr_element.value="";
- birth_yr_element.focus();
- birth_yr_element.style.background="#ff5555";
- valid= false;
- }
- return valid;
- }
- // This function is used to remove the error messages from the page when the user resubmits the form... e.g. if a user makes an error with fname and lname and tries to correct them both, however the system finds that there is still an error with the last name, it is important that they send the user only the error message for the last name on the second try. To accomplish this we simply clear all the errors each time the user attempts to submit the form and the validation code will reinsert any errors which were made on the subsequent submission.
- function clearErrors(){
- var errors = document.getElementsByClassName('error');
- for(var i = 0; i < errors.length; i++){
- errors[i].innerHTML = "";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement