Commit c20cb6a9b8f61556cf66ba4828c0fb9f246cc7cc
1 parent
98ac1668
Added javascript validation functions
git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@820 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing
1 changed file
with
163 additions
and
26 deletions
presentation/lookAndFeel/knowledgeTree/js/misc.js
| 1 | -function validateString(field, msg, min, max) { | ||
| 2 | - if (!min) { min = 1 } | ||
| 3 | - if (!max) { max = 65535 } | ||
| 4 | - | ||
| 5 | - if (!field.value || field.value.length < min || field.value.max > max) { | ||
| 6 | - alert(msg); | ||
| 7 | - field.focus(); | ||
| 8 | - field.select(); | ||
| 9 | - return false; | ||
| 10 | - } | ||
| 11 | - return true; | ||
| 12 | -} | ||
| 13 | - | ||
| 14 | -function validateNumber(field, msg, min, max) { | ||
| 15 | - if (!min) { min = 0 } | ||
| 16 | - if (!max) { max = 255 } | ||
| 17 | - | ||
| 18 | - if ( (parseInt(field.value) != field.value) || field.value.length < min || field.value.length > max) { | ||
| 19 | - alert(msg); | ||
| 20 | - field.focus(); | ||
| 21 | - field.select(); | ||
| 22 | - return false; | ||
| 23 | - } | ||
| 24 | - return true; | ||
| 25 | -} | ||
| 26 | - | ||
| 27 | function setActionAndSubmit(newAction) { | 1 | function setActionAndSubmit(newAction) { |
| 28 | document.MainForm.action = newAction; | 2 | document.MainForm.action = newAction; |
| 29 | document.MainForm.submit(); | 3 | document.MainForm.submit(); |
| 30 | } | 4 | } |
| 5 | + | ||
| 6 | +function isEmailAddr(email) | ||
| 7 | +{ | ||
| 8 | + var result = false; | ||
| 9 | + var theStr = new String(email); | ||
| 10 | + var index = theStr.indexOf("@"); | ||
| 11 | + if (index > 0) | ||
| 12 | + { | ||
| 13 | + var pindex = theStr.indexOf(".",index); | ||
| 14 | + if ((pindex > index+1) && (theStr.length > pindex+1)) | ||
| 15 | + result = true; | ||
| 16 | + } | ||
| 17 | + return result; | ||
| 18 | +} | ||
| 19 | + | ||
| 20 | +function validRequired(formField,fieldLabel) | ||
| 21 | +{ | ||
| 22 | + var result = true; | ||
| 23 | + | ||
| 24 | + if (formField.value.length == 0) | ||
| 25 | + { | ||
| 26 | + alert('Please enter a value for the "' + fieldLabel +'" field.'); | ||
| 27 | + formField.focus(); | ||
| 28 | + result = false; | ||
| 29 | + } | ||
| 30 | + return result; | ||
| 31 | + | ||
| 32 | +} | ||
| 33 | + | ||
| 34 | +function allDigits(str) | ||
| 35 | +{ | ||
| 36 | + return inValidCharSet(str,"0123456789"); | ||
| 37 | +} | ||
| 38 | + | ||
| 39 | +function inValidCharSet(str,charset) | ||
| 40 | +{ | ||
| 41 | + var result = true; | ||
| 42 | + | ||
| 43 | + // Note: doesn't use regular expressions to avoid early Mac browser bugs | ||
| 44 | + for (var i=0;i<str.length;i++) | ||
| 45 | + if (charset.indexOf(str.substr(i,1))<0) | ||
| 46 | + { | ||
| 47 | + result = false; | ||
| 48 | + break; | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + return result; | ||
| 52 | +} | ||
| 53 | + | ||
| 54 | +function validEmail(formField,fieldLabel,required) | ||
| 55 | +{ | ||
| 56 | + var result = true; | ||
| 57 | + | ||
| 58 | + if (required && !validRequired(formField,fieldLabel)) | ||
| 59 | + result = false; | ||
| 60 | + | ||
| 61 | + if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) ) | ||
| 62 | + { | ||
| 63 | + alert("Please enter a complete email address in the form: yourname@yourdomain.com"); | ||
| 64 | + formField.focus(); | ||
| 65 | + result = false; | ||
| 66 | + } | ||
| 67 | + | ||
| 68 | + return result; | ||
| 69 | + | ||
| 70 | +} | ||
| 71 | + | ||
| 72 | +function validNum(formField,fieldLabel,required) | ||
| 73 | +{ | ||
| 74 | + var result = true; | ||
| 75 | + | ||
| 76 | + if (required && !validRequired(formField,fieldLabel)) | ||
| 77 | + result = false; | ||
| 78 | + | ||
| 79 | + if (result) | ||
| 80 | + { | ||
| 81 | + if (!allDigits(formField.value)) | ||
| 82 | + { | ||
| 83 | + alert('Please enter a number for the "' + fieldLabel +'" field.'); | ||
| 84 | + formField.focus(); | ||
| 85 | + result = false; | ||
| 86 | + } | ||
| 87 | + } | ||
| 88 | + | ||
| 89 | + return result; | ||
| 90 | +} | ||
| 91 | + | ||
| 92 | + | ||
| 93 | +function validInt(formField,fieldLabel,required) | ||
| 94 | +{ | ||
| 95 | + var result = true; | ||
| 96 | + | ||
| 97 | + if (required && !validRequired(formField,fieldLabel)) | ||
| 98 | + result = false; | ||
| 99 | + | ||
| 100 | + if (result) | ||
| 101 | + { | ||
| 102 | + var num = parseInt(formField.value,10); | ||
| 103 | + if (isNaN(num)) | ||
| 104 | + { | ||
| 105 | + alert('Please enter a number for the "' + fieldLabel +'" field.'); | ||
| 106 | + formField.focus(); | ||
| 107 | + result = false; | ||
| 108 | + } | ||
| 109 | + } | ||
| 110 | + | ||
| 111 | + return result; | ||
| 112 | +} | ||
| 113 | + | ||
| 114 | + | ||
| 115 | +function validDate(formField,fieldLabel,required) | ||
| 116 | +{ | ||
| 117 | + var result = true; | ||
| 118 | + | ||
| 119 | + if (required && !validRequired(formField,fieldLabel)) | ||
| 120 | + result = false; | ||
| 121 | + | ||
| 122 | + if (result) | ||
| 123 | + { | ||
| 124 | + var elems = formField.value.split("/"); | ||
| 125 | + | ||
| 126 | + result = (elems.length == 3); // should be three components | ||
| 127 | + | ||
| 128 | + if (result) | ||
| 129 | + { | ||
| 130 | + var month = parseInt(elems[0],10); | ||
| 131 | + var day = parseInt(elems[1],10); | ||
| 132 | + var year = parseInt(elems[2],10); | ||
| 133 | + result = allDigits(elems[0]) && (month > 0) && (month < 13) && | ||
| 134 | + allDigits(elems[1]) && (day > 0) && (day < 32) && | ||
| 135 | + allDigits(elems[2]) && ((elems[2].length == 2) || (elems[2].length == 4)); | ||
| 136 | + } | ||
| 137 | + | ||
| 138 | + if (!result) | ||
| 139 | + { | ||
| 140 | + alert('Please enter a date in the format MM/DD/YYYY for the "' + fieldLabel +'" field.'); | ||
| 141 | + formField.focus(); | ||
| 142 | + } | ||
| 143 | + } | ||
| 144 | + | ||
| 145 | + return result; | ||
| 146 | +} | ||
| 147 | + | ||
| 148 | +/*function validateForm(theForm) | ||
| 149 | +{ | ||
| 150 | + // Customize these calls for your form | ||
| 151 | + | ||
| 152 | + // Start -------> | ||
| 153 | + if (!validRequired(theForm.fullname,"Name")) | ||
| 154 | + return false; | ||
| 155 | + | ||
| 156 | + if (!validEmail(theForm.email,"Email Address",true)) | ||
| 157 | + return false; | ||
| 158 | + | ||
| 159 | + if (!validDate(theForm.available,"Date Available",true)) | ||
| 160 | + return false; | ||
| 161 | + | ||
| 162 | + if (!validNum(theForm.yearsexperience,"Years Experience",true)) | ||
| 163 | + return false; | ||
| 164 | + // <--------- End | ||
| 165 | + | ||
| 166 | + return true; | ||
| 167 | +}*/ |