1// ---------------------------------------------------------------------- 2// Javascript form validation routines. 3// Author: Stephen Poley 4// 5// Simple routines to quickly pick up obvious typos. 6// All validation routines return true if executed by an older browser: 7// in this case validation must be left to the server. 8// 9// Update Jun 2005: discovered that reason IE wasn't setting focus was 10// due to an IE timing bug. Added 0.1 sec delay to fix. 11// 12// Update Oct 2005: minor tidy-up: unused parameter removed 13// 14// Update Jun 2006: minor improvements to variable names and layout 15// ---------------------------------------------------------------------- 16 17var nbsp = 160; // non-breaking space char 18var node_text = 3; // DOM text node-type 19var emptyString = /^\s*$/ ; 20var global_valfield; // retain valfield for timer thread 21 22// -------------------------------------------- 23// trim 24// Trim leading/trailing whitespace off string 25// -------------------------------------------- 26 27function trim(str) 28{ 29 return str.replace(/^\s+|\s+$/g, ''); 30} 31 32 33// -------------------------------------------- 34// setfocus 35// Delayed focus setting to get around IE bug 36// -------------------------------------------- 37 38function setFocusDelayed() 39{ 40 global_valfield.focus(); 41} 42 43function setfocus(valfield) 44{ 45 // save valfield in global variable so value retained when routine exits 46 global_valfield = valfield; 47 setTimeout( 'setFocusDelayed()', 100 ); 48} 49 50 51// -------------------------------------------- 52// msg 53// Display warn/error message in HTML element. 54// commonCheck routine must have previously been called 55// -------------------------------------------- 56 57function msg(fld, // id of element to display message in 58 msgtype, // class to give element ("warn" or "error") 59 message) // string to display 60{ 61 // setting an empty string can give problems if later set to a 62 // non-empty string, so ensure a space present. (For Mozilla and Opera one could 63 // simply use a space, but IE demands something more, like a non-breaking space.) 64 var dispmessage; 65 if (emptyString.test(message)) 66 dispmessage = String.fromCharCode(nbsp); 67 else 68 dispmessage = message; 69 70 var elem = document.getElementById(fld); 71 elem.firstChild.nodeValue = dispmessage; 72 73 elem.className = msgtype; // set the CSS class to adjust appearance of message 74} 75 76// -------------------------------------------- 77// commonCheck 78// Common code for all validation routines to: 79// (a) check for older / less-equipped browsers 80// (b) check if empty fields are required 81// Returns true (validation passed), 82// false (validation failed) or 83// proceed (don't know yet) 84// -------------------------------------------- 85 86var proceed = 2; 87 88function commonCheck (valfield, // element to be validated 89 infofield, // id of element to receive info/error msg 90 required) // true if required 91{ 92 if (!document.getElementById) 93 return true; // not available on this browser - leave validation to the server 94 var elem = document.getElementById(infofield); 95 if (!elem.firstChild) return true; // not available on this browser 96 if (elem.firstChild.nodeType != node_text) return true; // infofield is wrong type of node 97 98 if (emptyString.test(valfield.value)) { 99 if (required) { 100 msg (infofield, "error", "Empty Field"); 101 //setfocus(valfield); 102 return false; 103 } 104 else { 105 msg (infofield, "warn", ""); // OK 106 return true; 107 } 108 } 109 return proceed; 110} 111 112 113 114 115 116// -------------------------------------------- 117// validatePresent 118// Validate if something has been entered 119// Returns true if so 120// -------------------------------------------- 121 122function validatePresent(valfield, // element to be validated 123 infofield ) // id of element to receive info/error msg 124{ 125 var stat = commonCheck (valfield, infofield, true); 126 if (stat != proceed) 127 { 128 return stat; 129 } 130 else 131 { 132 msg (infofield, "warn", ""); 133 return true; 134}} 135 136// -------------------------------------------- 137// validateEmail 138// Validate if e-mail address 139// Returns true if so (and also if could not be executed because of old browser) 140// -------------------------------------------- 141 142function validateEmail (valfield, // element to be validated 143 infofield, // id of element to receive info/error msg 144 required) // true if required 145{ 146 var stat = commonCheck (valfield, infofield, required); 147 if (stat != proceed) return stat; 148 149 var tfld = trim(valfield.value); // value of field with whitespace trimmed off 150 var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ; 151 152 if (!email.test(tfld)) { 153 msg (infofield, "error", "You did not enter a valid email address. Please try again."); 154 setfocus(valfield); 155 return false;
156 } 157 if (emptyString.test(tfld)) { 158 159 msg (infofield, "error", "Empty Field"); 160 //setfocus(valfield); 161 return false; 162 } 163 164 var email2 = /^[A-Za-z][\w.-]+@\w[\w.-]+\.[\w.-]*[A-Za-z][A-Za-z]$/ ; 165 if (!email2.test(tfld)) 166 msg (infofield, "warn", "Pls. Check"); 167 else 168 msg (infofield, "warn", ""); 169 return true; 170} 171
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.