Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. // this is some jquery to say when the document is ready, run whatever is
  2. // inside this function
  3. $(function() {
  4.  
  5. // function to check whether the form is valid or not
  6. function isValid() {
  7. // removes the has-error class from anything that has it
  8. // basically, this removes all the error states from our form
  9. $('.has-error').removeClass('has-error')
  10.  
  11. // the form starts off valid, but if anything is invalid, it becomes false
  12. var valid = true
  13.  
  14. // saves the username input, label and text into variables
  15. var usernameInput = $("#username")
  16. var usernameLabel = $("#username-label")
  17. var username = usernameInput.val()
  18.  
  19. // same as above
  20. var avatarUrlInput = $("#avatar_url")
  21. var avatarUrlLabel = $("#avatar_url-label")
  22. var avatarUrl = avatarUrlInput.val()
  23.  
  24. // same as above
  25. var emailInput = $("#email")
  26. var emailLabel = $("#email-label")
  27. var email = emailInput.val()
  28.  
  29. // same as above
  30. var passwordInput = $("#password")
  31. var passwordLabel = $("#password-label")
  32. var password = passwordInput.val()
  33.  
  34. // checks if the username is null or blank
  35. if(username == null || username.length < 1) {
  36. // if username invalid, valid is false
  37. valid = false
  38. // add error class to input and label
  39. usernameInput.addClass('has-error')
  40. usernameLabel.addClass('has-error')
  41. }
  42.  
  43. // same as above, but with avatarUrl
  44. if(avatarUrl == null || avatarUrl.length < 1) {
  45. valid = false
  46. avatarUrlInput.addClass('has-error')
  47. avatarUrlLabel.addClass('has-error')
  48. }
  49.  
  50. // same as above, but with email
  51. if(email == null || email.length < 1) {
  52. valid = false
  53. emailInput.addClass('has-error')
  54. emailLabel.addClass('has-error')
  55. }
  56.  
  57. // same as above, but with password
  58. if(password == null || password.length < 1) {
  59. valid = false
  60. passwordInput.addClass('has-error')
  61. passwordLabel.addClass('has-error')
  62. }
  63.  
  64. // returns true if everything is valid, or false if one input isn't
  65. return valid
  66. }
  67.  
  68. // attaches a click event to the clear button, that sets the text
  69. // of all our inputs to blank
  70. $("#clear-button").on("click", function(ev) {
  71. // prevent default button functionality that would submit
  72. ev.preventDefault()
  73.  
  74. // removes all the errors
  75. $('.has-error').removeClass('has-error')
  76.  
  77. // gets all inputs in the form and sets their values to blank
  78. $('input[type="text"], input[type="email"], input[type="password"]').val('')
  79. })
  80.  
  81. // attaches a submit event to the form itself, so when it's submitted,
  82. // it will validate the form
  83. $("#signup-form").on("submit", function(ev) {
  84. // this requires some knowledge of submit events.
  85. //
  86. // so when a form submits, if the event returns true, the form will
  87. // be submitted and we'll POST to our server. if it returns false,
  88. // it won't send any data.
  89.  
  90. // therefore, if our form is valid, we'll return true and send the data,
  91. // otherwise we won't.
  92.  
  93. // NOTE: If someone disables JavaScript, the form will just submit :(
  94. return isValid()
  95. })
  96. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement