Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
- </head>
- <body>
- <div class="container">
- <h1>Form Validation</h1>
- <div class="row">
- <div class="col">
- <form>
- <div class="form-group row has-error">
- <label class="col-sm-2 col-form-label">Name</label>
- <div class="col-sm-10">
- <input type="text" class="form-control is-invalid" data-validate="name" placeholder="Name">
- <div class="invalid-feedback">
- Please provide a valid name.
- </div>
- </div>
- </div>
- <div class="form-group row">
- <label class="col-sm-2 col-form-label">Email</label>
- <div class="col-sm-10">
- <input type="text" class="form-control" data-validate="email" placeholder="Email">
- </div>
- </div>
- <div class="form-group row">
- <label class="col-sm-2 col-form-label">Age</label>
- <div class="col-sm-10">
- <input type="text" class="form-control" data-validate="age" placeholder="Age">
- </div>
- </div>
- <div class="form-group row">
- <div class="col-sm-10">
- <button type="submit" class="btn btn-primary">Sign in</button>
- </div>
- </div>
- </form>
- </div>
- <div class="col">
- <p>Your task is to create JavaScript + jQuery snippet for validation of inputs.</p>
- <h3>Validation rules:</h3>
- <ul>
- <li>Name has to be at least 3 letters</li>
- <li>Email has to contain '@'</li>
- <li>Age has to be a number greater than 13</li>
- </ul>
- <p> Hint: .length, indexOf(), isNaN() </p>
- </div>
- </div>
- </div>
- <script
- src="https://code.jquery.com/jquery-3.2.1.min.js"
- integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
- crossorigin="anonymous"></script>
- <script>
- /*
- Deconstruct problem
- 1) select input to listen for changes
- 2) get actual value of input
- 3) determine the type of validation
- 4) do the validation
- 5) show the results of validation (message, add / remove class)
- */
- //1
- $('input').keyup(function(){
- /*console.log($(this));*/
- //takes the input from user and puts it into the variable value using the function .val()
- var value = $(this).val();//2
- //gets the input from the attribute data-validate using the .data function | could also use:
- //.attr('id') but not as good;
- var validate = $(this).data('validate');//3
- //4
- var valid = true;
- if( validate == 'name'){
- valid = value.length >= 3;
- }else if (validate == 'email'){
- //fast way of checking if @ is in string - if not there it is not equal to -1
- valid = value.indexOf('@') != -1;
- }else if ( validate == 'age'){
- valid = !isNan(value) && value >= 13;
- }
- // add validation for email and age
- // indexOf(), isNaN()
- //5
- if(valid){
- $(this).removeClass('is-invalid');
- $(this).siblings('.invalid-feedback').remove();
- }else{
- $(this).addClass('is-invalid');
- if( $(this).siblings('.invalid-feedback').length == 0 ){
- $(this).after('<div class="invalid-feedback">Please provide a valid name.</div>')
- }
- }
- });
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement