Migration SVN
This commit is contained in:
230
web/javascript/forms.js
Executable file
230
web/javascript/forms.js
Executable file
@@ -0,0 +1,230 @@
|
||||
// Critical error, when exception is captured
|
||||
var critical_error = 'Critical error : The script has generated an error. Our team was informed.';
|
||||
// Regular exp to use
|
||||
var field_format = {
|
||||
'last_name': /^[a-zA-Z., -]{1,35}$/,
|
||||
'first_name': /^[a-zA-Z., -]{1,35}$/,
|
||||
'company': /^[a-zA-Z0-9.,& -]{0,35}$/,
|
||||
'address': /^[a-zA-Z0-9.,& -]{0,250}$/,
|
||||
'city': /^[a-zA-Z -]{1,35}$/,
|
||||
'zipcode': /^[a-zA-Z0-9-]{1,12}$/,
|
||||
'email': /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/,
|
||||
'pseudo': /^[a-z0-9]{1,9}$/,
|
||||
'id': /^[0-9]{1,11}$/,
|
||||
'template': /^[a-zA-Z]{0,20}$/,
|
||||
'password': /^[^'"]{4,15}$/,
|
||||
'new_password1': /^[^'"]{4,15}$/,
|
||||
'new_password2': /^[^'"]{4,15}$/,
|
||||
'subject': /^[a-z0-9éèàçù].{4,99}$/i, // 5 to 100 chars, begining with an alphanumeric one
|
||||
'message': /^[a-z0-9éèàçù].{4,499}$/mi, // 5 to 500 chars, begining with an alphanumeric one, multiline
|
||||
'url': /^(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&\/~\+#]*[\w\-\@?^=%&\/~\+#])?$/i,
|
||||
'frequency': /^[0-9]{1,11}$/,
|
||||
'first_start': /^[0-9]{1,11}$/,
|
||||
'user_folder': /^\/{1}([a-z0-9_-]*\.?[a-z0-9_-]*\/{1})?$/i,
|
||||
'host': /^([a-z0-9]([a-z0-9\-]{0,61}[a-z0-9])?\.)+[a-z]{2,6}$/i,
|
||||
'sql_def_host': /^([a-z0-9]([a-z0-9\-]{0,61}[a-z0-9])?\.)+[a-z]{2,6}$/i,
|
||||
'sql_def_user': /^[a-z0-9]{1,9}$/,
|
||||
'sql_def_pwd': /^[^'"]{4,15}$/
|
||||
}
|
||||
// Relatives paths
|
||||
var rel_path = {
|
||||
0: '.',
|
||||
1: '..',
|
||||
2: '../..'
|
||||
}
|
||||
|
||||
// Adding the 'trim' method to class String
|
||||
String.prototype.trim = function()
|
||||
{
|
||||
return this.replace(/(^\s*)|(\s*$)/g,'');
|
||||
}
|
||||
|
||||
function checkRequiredState(field)
|
||||
{
|
||||
var id=field.id.toLowerCase();
|
||||
// If the field ID doesn't start with 'req', this field is not required
|
||||
// We can stop here and return true
|
||||
if (id.indexOf('req_')!=0) return true;
|
||||
var ftype=field.type;
|
||||
var fval=$('#'+id).val();
|
||||
fval.trim();
|
||||
if ((ftype == 'text') || (ftype == 'password') || (ftype == 'textarea')) {
|
||||
if (fval=='') return false;
|
||||
} else if (ftype == 'select-one') {
|
||||
if (fval == '') return false;
|
||||
if (parseInt(fval) == -1) return false;
|
||||
} else if (ftype == 'checkbox') {
|
||||
if (field.checked == false) return false;
|
||||
} else {
|
||||
// If we come here, the field type hasn't been trated yet
|
||||
// Returning false for security
|
||||
return false;
|
||||
}
|
||||
// If we come here, the field is ok
|
||||
return true;
|
||||
}
|
||||
|
||||
function checkFieldFormat(field)
|
||||
{
|
||||
var id=field.id.toLowerCase();
|
||||
var fval=$('#'+id).val();
|
||||
// If the field is empty and not required, we don't have to make the verification
|
||||
if ((fval=='') && (id.indexOf('req_')!=0)) return true;
|
||||
if (typeof(field_format[id])!='undefined') {
|
||||
if ( field_format[id].exec(fval) ) return true;
|
||||
else return false;
|
||||
} else if ( id.indexOf('_usrfldr') != -1) {
|
||||
if ( field_format['user_folder'].exec(fval) ) return true;
|
||||
else return false;
|
||||
} else if (id.indexOf('req_')==0) {
|
||||
id=id.substr(4);
|
||||
if ( typeof(field_format[id])!='undefined') {
|
||||
if ( field_format[id].exec(fval) ) return true;
|
||||
else return false;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function validateForm( form_id , relativePath)
|
||||
{
|
||||
var all_is_ok = true;
|
||||
// Closing the results area
|
||||
$('#result_datas').slideUp('slow');
|
||||
$('#result_datas > p').each(function() { $('#'+this.id).slideUp(); });
|
||||
|
||||
$('#'+form_id+' :input').each(
|
||||
function ()
|
||||
{
|
||||
var field_id=this.id;
|
||||
if (( this.type != 'button' ) && ( this.type != 'radio' ) && (this.type != 'hidden')) {
|
||||
var is_ok=checkRequiredState(this);
|
||||
if (is_ok==false) {
|
||||
all_is_ok=false;
|
||||
$('#err_'+field_id+'_missing').slideDown('slow');
|
||||
$('#err_'+field_id).slideUp('slow');
|
||||
} else {
|
||||
$('#err_'+field_id+'_missing').slideUp('slow');
|
||||
if ((this.type!='checkbox') && (this.type!='select-one')) {
|
||||
is_ok=checkFieldFormat(this);
|
||||
if (is_ok==false) {
|
||||
all_is_ok=false;
|
||||
$('#err_'+field_id).slideDown('slow');
|
||||
} else {
|
||||
$('#err_'+field_id).slideUp('slow');
|
||||
}
|
||||
}
|
||||
}
|
||||
var css_class1 = (is_ok==true?'success':'error');
|
||||
var css_class2 = (is_ok==true?'error':'success');
|
||||
|
||||
if (this.type != 'checkbox') {
|
||||
css_class1 = 'b' + css_class1;
|
||||
css_class2 = 'b' + css_class2;
|
||||
field_id = '#' + field_id;
|
||||
} else {
|
||||
field_id = '#lbl_' + field_id;
|
||||
}
|
||||
if ($(field_id).hasClass(css_class2)) $(field_id).removeClass(css_class2);
|
||||
if (! $(field_id).hasClass(css_class1)) $(field_id).addClass(css_class1);
|
||||
}
|
||||
});
|
||||
// If there was errors at verification time, we stop here
|
||||
if (all_is_ok==false) {
|
||||
$('#result_datas').slideDown('slow');
|
||||
return false;
|
||||
}
|
||||
if (relativePath==undefined) {
|
||||
relativePath=0;
|
||||
} else if (typeof(rel_path[relativePath])=='undefined') {
|
||||
relativePath=0;
|
||||
}
|
||||
var strurl=rel_path[relativePath];
|
||||
strurl += '/ajax.php?frm_id=' + form_id;
|
||||
// Executing the request
|
||||
var frm_datas = $('#'+form_id+' :input').serialize();
|
||||
$.ajax({
|
||||
url: strurl,
|
||||
data: frm_datas,
|
||||
type: 'POST',
|
||||
start: $('#'+ form_id +' :input').attr('disabled','disabled'),
|
||||
error: function(responseText) { alert(critical_error);},
|
||||
success: function(responseText){displayFormResults( form_id, responseText);},
|
||||
});
|
||||
}
|
||||
|
||||
function displayFormResults(form_id,responseText)
|
||||
{
|
||||
var hide_form=true;
|
||||
|
||||
if (responseText == 'critical_error') {
|
||||
alert(critical_error);
|
||||
return;
|
||||
}
|
||||
responseText = eval(responseText);
|
||||
// If the first code is 1, we can hide the form
|
||||
// Elsewhere, we do not hide it
|
||||
if (responseText[0]!=1) hide_form=false;
|
||||
|
||||
for (var i = 1; i < responseText.length; i++)
|
||||
{
|
||||
if ( isNaN(responseText[i]))
|
||||
$('#'+responseText[i]).slideDown();
|
||||
else
|
||||
$('#ret_code_'+responseText[i]).slideDown();
|
||||
}
|
||||
if ( hide_form ) {
|
||||
$('#'+form_id+'_container').slideUp("slow", function(){$('#result_datas').slideDown("slow");});
|
||||
} else {
|
||||
$('#'+form_id+' select,input,textarea').attr('disabled','');
|
||||
$('#result_datas').slideDown("slow");
|
||||
}
|
||||
}
|
||||
|
||||
function doSingleRequest(reqID, value, onSuccess, onFailure, relativePath)
|
||||
{
|
||||
// Checking values of callbacks
|
||||
if (onSuccess == undefined || onSuccess == '') {
|
||||
alert('onSuccess callback undefined !');
|
||||
return false;
|
||||
}
|
||||
if (onFailure == undefined || onSuccess == '') {
|
||||
alert('onFailure callback undefined !');
|
||||
return false;
|
||||
}
|
||||
|
||||
if (relativePath==undefined) {
|
||||
relativePath=0;
|
||||
} else if (typeof(rel_path[relativePath])=='undefined') {
|
||||
relativePath=0;
|
||||
}
|
||||
|
||||
var strurl=rel_path[relativePath];
|
||||
strurl += '/ajax.php?frm_id=' + reqID;
|
||||
var datas='value=' + value;
|
||||
$.ajax({
|
||||
url: strurl,
|
||||
data: datas,
|
||||
type: 'POST',
|
||||
error: function(responseText) { alert(critical_error);},
|
||||
success: function(responseText){displaySingleRequestResults( responseText, onSuccess, onFailure);},
|
||||
});
|
||||
}
|
||||
|
||||
function displaySingleRequestResults(results, onSuccess, onFailure)
|
||||
{
|
||||
if (results == 'critical_error') {
|
||||
alert(critical_error);
|
||||
return;
|
||||
}
|
||||
results = eval(results);
|
||||
// If the first code is 1, we call the onSuccess function
|
||||
// Elsewhere, we call the onFailure function
|
||||
if (results[0]==1) {
|
||||
onSuccess(results);
|
||||
} else {
|
||||
onFailure(results);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user