// THIS CODE IS COPIED IN SERVER_SCRIPTS.ASP because ASP // doesn't let you include things happily. // If you change anything here, change it in server_scripts.asp as well. // It used to be in a file called isvalid.asp // *********** Filename: isvalid.asp *********** // The "isvalid_*" functions check for various kinds of possible inputs, // and returns true if it is valid, false if there's an error. Also // appends to the errorList that will eventually get shown to the user. // As they are used in the automatically generated javascript, the return values // of the isvalid* functions aren't really relevant. In the end, validate() will // return true or false depending on whether the global variable errorList has been // modified (which happens whenever an isvalid* function finds something wrong). // But they return true and false values so that people can use them separately // if desired. function isvalid_alpha_num(field_name, test_value, description, min_val, max_val, null_ok) { if ( (null_ok == true) && (test_value == "")) { return true; } else if ( ( null_ok == false) && (test_value == "") ) { errorList.Add(field_name, description + " cannot be blank.\n"); return false; } else if ( !isNaN(min_val) && test_value.length < min_val ) { errorList.Add(field_name, description + " must be at least " + min_val + " characters long.\n"); return false; } else if ( !isNaN(max_val) && test_value.length > max_val ) { errorList.Add (field_name, description + " cannot be greater than " + max_val + " characters long.\n"); return false; } return true; } // Check for a valid number, falling between a certain range function isvalid_num(field_name, test_value, description, min_val, max_val, null_ok) { if ( (null_ok == true) && (test_value == "")) { return true; } else if ( isNaN(parseFloat(test_value)) || isNaN(test_value) ) { errorList.Add(field_name, description + " requires a valid number.\n"); return false; } else if ( (!isNaN(min_val)) && ( parseFloat(test_value) < min_val) ) { errorList.Add(field_name, description + " must not be less than than " + min_val + ".\n"); return false; } else if ( (!isNaN(max_val)) && ( parseFloat(test_value) > max_val) ) { errorList.Add(field_name, description + " must not be greater than " + max_val + ".\n"); return false; } return true; } // Check for a valid integer, falling between a certain range function isvalid_int(field_name, test_value, description, min_val, max_val, null_ok) { var str = new String(test_value); if ( (null_ok == true) && (str == "")) { return true; } else if ( isNaN(str) || (str.search(/\./) != -1) || isNaN(parseInt(str)) ) { errorList.Add(field_name, description + " requires an integer value.\n"); return false; } else if ( (!isNaN(min_val)) && (parseInt(str) < min_val) ) { errorList.Add(field_name, description + " must not be less than than " + min_val + ".\n"); return false; } else if ( (!isNaN(max_val)) && ( parseInt(str) > max_val) ) { errorList.Add(field_name, description + " must not be greater than " + max_val + ".\n"); return false; } return true; } // Check for valid currency amount, falling between a certain range // valid formats: 25, 25.7, 25.75 function isvalid_currency(field_name, test_value, description, min_val, max_val, null_ok) { if ( (null_ok == true) && (test_value == "")) { return true; } // See if it's even a valid number, and check to make sure it only goes to hundredths else if ( isNaN(parseFloat(test_value)) || ( (100 * test_value) % 1 != 0 ) ) { errorList.Add(field_name, description + " is not a valid currency.\n"); return false; } else if ( (!isNaN(min_val)) && ( parseFloat(test_value) < min_val) ) { errorList.Add(field_name, description + " must not be less than than " + min_val + ".\n"); return false; } else if ( (!isNaN(max_val)) && ( parseFloat(test_value) > max_val) ) { errorList.Add(field_name, description + " must not be greater than " + max_val + ".\n"); return false; } return true; } // Check for valid e-mail Address function isvalid_email(field_name, test_value, description, min_val, max_val, null_ok) { var email = /([\w.\-]+)@([\w.\-]+)\.([\w.\-]+)/; // This is a regexp pattern match for an e-mail var str = new String(test_value); if ( (null_ok == true) && (str == "")) { return true; } else if ( !str.match(email) ) { errorList.Add(field_name, description + " requires an e-mail Address.\n"); return false; } else if ( !isNaN(min_val) && str.length < min_val ) { errorList.Add(field_name, description + " must be at least " + min_val + " characters long.\n"); return false; } else if ( !isNaN(max_val) && str.length > max_val ) { errorList.Add (field_name, description + " cannot be greater than " + max_val + " characters long.\n"); return false; } return true; } // Check for valid url function isvalid_url(field_name, test_value, description, min_val, max_val, null_ok) { var url = /(\w+):\/\/([\w.]+)(\S*)/; // This is a regexp pattern match for a url var str = new String(test_value); if ( (null_ok == true) && (str == "")) { return true; } else if ( !str.match(url) ) { errorList.Add(field_name, description + " requires a valid URL.\n"); return false; } else if ( !isNaN(min_val) && str.length < min_val ) { errorList.Add(field_name, description + " must be at least " + min_val + " characters long.\n"); return false; } else if ( !isNaN(max_val) && str.length > max_val ) { errorList.Add (field_name, description + " cannot be greater than " + max_val + " characters long.\n"); return false; } return true; } function isvalid_date(field_name, test_value, description, min_val, max_val, null_ok) { var str = new String(test_value); if ( (null_ok == true) && (str == "//")) { return true; } // date_arr[0] is the month, [1] is the date, [2] is the year var date_arr = test_value.split("/"); var the_month = date_arr[0]; var the_date = date_arr[1]; var the_year = date_arr[2]; if ( isNaN(the_month) || isNaN(the_date) || isNaN(the_year) ) { errorList.Add(field_name, description + " is not a valid date.\n"); return false; } var the_parsed_date = new Date(); the_parsed_date.setHours(0); the_parsed_date.setMinutes(0); the_parsed_date.setSeconds(0); the_parsed_date.setMilliseconds(0); the_parsed_date.setFullYear(0); the_parsed_date.setMonth(0); the_parsed_date.setDate(1); the_parsed_date.setFullYear(the_year); the_parsed_date.setMonth(the_month - 1); the_parsed_date.setDate(the_date); var the_max_date = new Date(max_val); var the_min_date = new Date(min_val); if ( (the_parsed_date.getMonth() != (the_month - 1) ) || // JavaScript months are 0 based (the_parsed_date.getFullYear() != the_year ) ) { // Then either the user put in too many days for a month (like 31 for Feb) // or somebody's trying to throw garbage data into the database. Either way... errorList.Add(field_name, description + " is not a valid date.\n"); return false; } if ( the_parsed_date.getTime() < the_min_date.getTime() ) { the_min_date_text = (the_min_date.getMonth() + 1) + "/" + the_min_date.getDate() + "/" + the_min_date.getFullYear(); errorList.Add(field_name, description + " must not be earlier than " + the_min_date_text + ".\n"); return false; } if ( the_parsed_date.getTime() > the_max_date.getTime() ) { the_max_date_text = (the_max_date.getMonth() + 1) + "/" + the_max_date.getDate() + "/" + the_max_date.getFullYear(); errorList.Add(field_name, description + " must not be later than " + the_max_date_text + ".\n"); return false; } return true; } // Check for a valid zip_code function isvalid_zip(field_name, test_value, description, min_val, max_val, null_ok) { var zip = /^\d{5}([-]\d{4})?$/; // This is a regexp pattern match for a zipcode. var str = new String(test_value); if ( (null_ok == true) && (str == "")) { return true; } else if ( !str.match(zip) ) { errorList.Add(field_name, description + " requires a zip code.\n"); return false; } else if ( str.length > max_val) { errorList.Add(field_name, description + " cannot be greater than " + max_val + " characters long.\n" ); return false; } return true; } // Mimimalist phone number checker. There are too many variations (international, parens or // not, dashes or not, etc.) and different sites are inclined to put them into the db in // differing manners. function isvalid_phone_num(field_name, test_value, description, min_val, max_val, null_ok) { var bad_phone_num = /[^\w\(\)\-]/; var str = new String(test_value); if ( (null_ok == true) && (str == "")) { return true; } else if ( str.match(bad_phone_num) ) { errorList.Add(field_name, description + " requires a phone number.\n"); return false; } else if ( str.length < min_val) { errorList.Add(field_name, description + " cannot be less than " + min_val + " characters long.\n" ); return false; } else if ( str.length > max_val) { errorList.Add(field_name, description + " cannot be greater than " + max_val + " characters long.\n" ); return false; } return true; } // ------------------------- Dictionary Object -------------------------------// // Since standard client side JavaScript doesn't have the Dictionary Object which we // use to list error messages, it's up to us to create one. Appending to a simple // string variable would have been easier, but it was done this way to be more flexible. // This way, you can look up to see if something went wrong with a specific form field // and perhaps take a separate action based on that information. // This also lets us share the error dictionary with the server side database error // list. A convenient place to find everything that goes wrong. // We Add entries by simply specifying object.key_val // to Add and giving a value. JavaScript's loose about it. function ClientDictObject() { this.Add = Add; return this; } // Add is a member function of ClientDictObjects. Note to self -- look into using // prototyping to save memory function Add(key_val, return_val) { this[key_val] = return_val; } function get_val(field) { var the_form_field = eval("document." + field); if (the_form_field.type == "select-one") { return the_form_field.options[the_form_field.selectedIndex].value; } return the_form_field.value; } function get_date(field) // field is in the format "formname.fieldname" { // Returns a date as a string; // The string is returned in MM/DD/YYYY format, which Netscape doesn't always like to // parse. (i.e. don't say: // // var my_date = new Date( get_date(field_name) ); // // Netscape doesn't like it. This is used by isvalid_date() and returns a string // because the date object will let you put things like 12/32/2000 (it becomes 1/1/2001) // and that should be checked for in an isvalid function. var date_arr = new Array(); var the_form_field = eval("document." + field); for (var i=0; i < 3; i++) { date_arr[i] = the_form_field[i].options[the_form_field[i].selectedIndex].value; } var the_date = new String(date_arr[0] + "/" + date_arr[1] + "/" + date_arr[2]); return the_date; } function process_errors() { var error_msg = ""; // A string to dump the errors into for (var i in errorList) { if (i != "Add") // Because we don't want the function "Add" to show up as an error { error_msg += errorList[i]; } } if ( error_msg != "") // The error_msg isn't empty -- something went wrong { error_msg = "The form could not be processed due to the following errors:\n\n" + error_msg; alert(error_msg); return false; } // We got through the whole list and found nothing wrong return true; } // General purpose functions: function get_date_obj(date_text) { var str = new String(date_text); // date_arr[0] is the month, [1] is the date, [2] is the year var date_arr = str.split("/"); var the_month = parseInt(date_arr[0]); var the_date = parseInt(date_arr[1]); var the_year = parseInt(date_arr[2]); var the_parsed_date = new Date(); the_parsed_date.setHours(0); the_parsed_date.setMinutes(0); the_parsed_date.setSeconds(0); the_parsed_date.setMilliseconds(0); the_parsed_date.setFullYear(the_year); the_parsed_date.setMonth(the_month - 1); the_parsed_date.setDate(the_date); return the_parsed_date; } //-------------------------------- errorList -----------------------------------// // errorList is the variable that builds the error message we send to the user. // In the end, we check the value of errorList, and validate() returns false // if we've added any errors. errorList is declared globally here so that it the isvalid // functions can add to it. It's initialized in validate() however, since we want to // clear out the errorList every time the user tries to submit the form. var errorList; // // // //