Advertisement
bcmoney

RegEx in HTML5 pattern .vs. JS RegEx

Nov 25th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Client-side Form Validation via JS (RegEx)</title>
  5. <style type="text/css">
  6. label, button, input[type=submit] {
  7. display: block;
  8. }
  9. </style>
  10. </head>
  11. <body>
  12. <div id="main">
  13. <form name="myForm" class="validatedForm" action="/action_page.php" onsubmit="return validateForm(event);" method="post">
  14. <fieldset>
  15. <label>Name: <input type="text" name="fullName" /></label>
  16. <label for="country">Country</label>
  17. <select id="country" name="country" title="Note: we are working hard on making our services available in more countries/language very soon!">
  18. <option value="-1" selected>[choose country]</option>
  19. <option value="1">CANADA</option>
  20. <option value="2">USA</option>
  21. <option value="3">UK</option>
  22. </select>
  23. <input type="submit" id="signupButton" name="signup" value="Sign Up" />
  24. </fieldset>
  25. </form>
  26. </div>
  27. <script>
  28. /* FORM validations */
  29. function showError(ele) {
  30. ele.style.border = '1px solid red';
  31. ele.focus();
  32. }
  33.  
  34. function clearError(ele) {
  35. ele.style.border = '1px solid transparent';
  36. document.getElementById('signup').focus();
  37. }
  38.  
  39. function validateName() {
  40. var name = document.forms['myForm']['fullName'].value;
  41. var validName = /^[a-zA-Z]+(([',. -][a-zA-Z ])?[a-zA-Z]*)*$/g;
  42. if (typeof name === 'undefined' || name === '') {
  43. alert('Name must be filled out');
  44. //return false;
  45. } else if (name.match(validName)) {
  46. alert('Hi: ' + name);
  47. //return false;
  48. } else {
  49. //return false;
  50. }
  51. }
  52.  
  53. function validateEmail() {
  54. var emailID = document.myForm.EMail.value;
  55. atpos = emailID.indexOf("@");
  56. dotpos = emailID.lastIndexOf(".");
  57.  
  58. if (atpos < 1 || ( dotpos - atpos < 2 )) {
  59. alert("Please enter correct email ID")
  60. document.myForm.EMail.focus() ;
  61. return false;
  62. }
  63.  
  64. return true;
  65. }
  66.  
  67. function validateForm(e) {
  68. e.preventDefault();
  69. if(document.forms['myForm']['fullName'].value === '') {
  70. alert('Please provide your name!');
  71. document.myForm.fullName.focus(); //notice we can access the form several ways, this is the original way by the name of the FORM element and field as a multi-dimensional Array
  72. return false;
  73. }
  74. if(document.getElementsByTagName('form')[0].email.value == '') {
  75. alert('Please provide your Email!');
  76. document.getElementsByTagName('form')[0].email //depends on the order in which this form appears, and total number of FORM elemetns on the page, so not great
  77. return false;
  78. }
  79. if(document.getElementsByClassName('validatedForm')[0].country.value === '-1') {
  80. alert('Please select your country!');
  81. document.getElementsByClassName('validatedForm')[0].country.focus(); //CSS class names used for styling can be a bit more specific but potentially suffer similar problems to using the number and ordering of the FORM, in this case there may accidentally be multiple Elements on the page with that class applied
  82. return false;
  83. }
  84. if(document.getElementById('postalCode').value === '' ||
  85. isNaN(document.getElementById('postalCode').value) ||
  86. document.myForm.Zip.value.length < 5) {
  87. alert('Please provide a Canadian postal code in the format L#L #L# (L=letter, #=number)');
  88. document.getElementById('postalCode').focus(); //using IDs is typically fairly reliable to retrieve individual fields, as we're supposed to be certain that each ID assigned to an Element on the paeg is unique, however, its not always the case if there are 3rd party integrations/widgets or coding errors and oversights
  89. return false;
  90. }
  91. if(document.querySelector('#newsletterOptIn').value === '' ||
  92. isNaN(document.getElementById('postalCode').value) ||
  93. document.myForm.Zip.value.length < 5) {
  94. alert('Please provide a Canadian postal code in the format L#L #L# (L=letter, #=number)');
  95. document.getElementById('postalCode').focus(); //using IDs is typically fairly reliable to retrieve individual fields, as we're supposed to be certain that each ID assigned to an Element on the paeg is unique, however, its not always the case if there are 3rd party integrations/widgets or coding errors and oversights
  96. return false;
  97. }
  98. return true;
  99. }
  100. </script>
  101. </body>
  102. </html>
  103.  
  104.  
  105. https://www.w3schools.com/js/tryit.asp?filename=tryjs_validation_js
  106. https://www.geeksforgeeks.org/javascript-match/
  107. https://www.tutorialspoint.com/javascript/javascript_form_validations.htm
  108.  
  109.  
  110. https://webdesign.tutsplus.com/tutorials/html5-form-validation-with-the-pattern-attribute--cms-25145
  111. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/text
  112.  
  113. PATTERN example:
  114. <input type="text" id="name" name="name" required minlength="4" maxlength="8" size="10" value="test" pattern="^[a-zA-Z]+(([',. -][a-zA-Z ])?[a-zA-Z]*)*$" />
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement