improved js validation tests (and reduced server charge)

This commit is contained in:
Loïc Chapeaux
2001-08-07 17:54:48 +00:00
parent ca2e2f534e
commit a681a9b6ce
3 changed files with 111 additions and 12 deletions

View File

@@ -1,13 +1,93 @@
/* $Id$ */
/**
* Displays an error message if the user submitted the sql query form with no
* sql query
*
* @param object the form
*
* @return boolean always false
*/
function emptySqlQuery(theForm)
{
var sqlQuery1 = theForm.elements['sql_query'];
var isRegExp = (typeof(sqlQuery1.value.replace) != 'undefined');
// The replace function (js1.2) isn't supported -> basic tests
if (!isRegExp) {
var isEmpty = (sqlQuery1.value == '') ? 1 : 0;
if (isEmpty && typeof(theForm.elements['sql_file']) != 'undefined') {
isEmpty = (theForm.elements['sql_file'].value == '') ? 1 : 0;
}
if (isEmpty && typeof(theForm.elements['id_bookmark']) != 'undefined') {
isEmpty = (theForm.elements['id_bookmark'].value == '') ? 1 : 0;
}
}
// js1.2+ -> validation with regular expressions
else {
var isEmpty = (sqlQuery1.value.replace(/\s/g, '') == '') ? 1 : 0;
if (isEmpty && typeof(theForm.elements['sql_file']) != 'undefined') {
isEmpty = (theForm.elements['sql_file'].value.replace(/\s/g, '') == '') ? 1 : 0;
}
if (isEmpty && typeof(theForm.elements['id_bookmark']) != 'undefined') {
isEmpty = (theForm.elements['id_bookmark'].value == '') ? 1 : 0;
}
if (isEmpty) {
theForm.reset();
}
}
if (isEmpty) {
sqlQuery1.select();
alert(errorMsg0);
sqlQuery1.focus();
return false;
}
return true;
} // end of the 'emptySqlQuery()' function
/**
* Displays an error message if an element of a form hasn't been completed and
* should be
*
* @param object the form
* @param string the name of the form field to put the focus on
*
* @return boolean whether the form field is empty or not
*/
function emptyFormElements(theForm, theFieldName)
{
var theField = theForm.elements[theFieldName];
// Whether the replace function (js1.2) is supported or not
var isRegExp = (typeof(theField.value.replace) != 'undefined');
if (!isRegExp) {
var isEmpty = (theField.value == '') ? 1 : 0;
} else {
var isEmpty = (theField.value.replace(/\s/g, '') == '') ? 1 : 0;
}
if (isEmpty) {
theForm.reset();
theField.select();
alert(errorMsg0);
theField.focus();
return false;
}
return true;
} // end of the 'emptyFormElements()' function
/**
* Ensures a value submitted in a form is numeric and is in a range
*
* @param object the form
* @param string the name of the form field to check
* @param integer the minimum authorized value + 1
* @param integer the maximum authorized value + 1
* @param integer the minimum authorized value
* @param integer the maximum authorized value
*
* @return boolean whether a valid number has been submitted or not
*/