// General JavaScript Validator // // author: Myles Chippendale, 1999 // // This script validates fields input from a form by checking // // 1) if the field is empty or not // 2) if the field matches a given regular expression // 3) if the field passes any other validation you wish to perform // in an arbitary javascript function // // To get a field to be validated, you need to call the add_rule // function (see below) with appropriate arguments. You can also // use the utility functions below for common validation functions // e.g. add_date("field", "Your Field", 1); // // You also need to add the following to any submit buttons: // // onClick="return validate(this.form)" // Strings gathered in one place for ease of internationalisation. // Note spaces are important. var is_not_defined_for_this_form = "n'est pas défini pour cette forme"; var must_be_filled_in = "Doit être complété"; var the_value = "La valeur"; var is_not_allowed = "on ne permet pas"; var there_were_problems = "Cette forme n'a pas été soumise parce qu'il y avait des problèmes avec les champs suivants"; var continue_anyway = "Voulez-vous continuer de toute façon ?"; var correct_and_try_again = "Svp le corret ces en essayent encore."; var rules = new Array(); var rulecount = 0; var extras = new Array(); var extracount = 0; // Function add_rule(name, heading, mandatory, pattern, snippet) // // name - name of the input field to validate // heading - what to call the field for the user's benefit // mandatory - 0,"0",false or null object means the field can be blank // pattern - a regular expression which any value entered into the // HTML form must match against for validation to succeed. // We need to escape special characters with '\', including '\' // itself (e.g. use \\s in your string if you want to match a // whitespace char - we want the expression to contain the 2 // characters '\s', not s. If you put brackets into your // expression then JavaScript will allow you to access them // through the global (to each window) variable RegExp as // RegExp.$1 ... RegExp.$9 in any javascript (for instance, // the next argument :-) // snippet - the body of JavaScript function. You can use this to perform // more general validation tests than just matching a regular // expression. It has access to one formal paramater, 'value' - // the value of the input field being validated. It also has // access to global variables such as RegExp.$1. It should // return nothing (or an empty string) if validation succeeds, // otherwise it should return a string to be shown to the user // explaining the problem. function add_rule(name, heading, mandatory, pattern, snippet) { rules[rulecount] = new Object(); rules[rulecount].name = name; rules[rulecount].heading = heading; rules[rulecount].pattern = pattern; rules[rulecount].mandatory = mandatory; rules[rulecount].snippet = snippet; rulecount++; } function add_extra(expression) { extras[extracount++] = expression; } // validate // // This takes a form object as input and checks each rule added using // add_rule. If there are any problems with validation, an alert box // is popped up and the form is not submitted. If there are problems // and allowSubmit is "yes" then the user will be presented with a // confirm dialog box (with "OK" and "Cancel" options) which will // submit the form anyway if OK is clicked. // // Previously this function had a test for the presence of // the RegExp object, but this is now taken for granted, as it has // been implemented since Netscape 4.0 // function validate (form, allowSubmit) { var display = ""; var name,heading,snippet,mandatory,value,re,func_result; // Loop through our rules, doing the appropriate checks for(i=0;i31 || RegExp.$1==0 || RegExp.$2>12 || RegExp.$2==0)?'" + the_value + " \"'+value+'\" " + is_not_allowed + "':'';" ); } // DateTime - dd/mm/yyyy hh:mm function add_datetime(name, heading, mandatory) { add_rule(name, heading, mandatory, "(^\\d{1,2})[/](\\d{1,2})[/]\\d{2,4}\\s*(\\d{1,2}):(\\d{1,2})\\s*$", "return (RegExp.$1>31 || RegExp.$1==0 || RegExp.$2>12 || RegExp.$2==0 || RegExp.$3>23 || RegExp.$4>59 )?'" + the_value + " \"'+value+'\" " + is_not_allowed + "':'';" ); } // Timestamp - dd/mm/yyyy hh:mm:ss.mmm function add_timestamp(name, heading, mandatory) { add_rule(name, heading, mandatory, "(^\\d{1,2})[/](\\d{1,2})[/]\\d{2,4}\\s*(\\d{1,2}):(\\d{1,2}):(\\d{1,2})\\s*$", "return (RegExp.$1>31 || RegExp.$1==0 || RegExp.$2>12 || RegExp.$2==0 || RegExp.$3>23 || RegExp.$4>59 || RegExp.$5>59 )?'" + the_value + " \"'+value+'\" " + is_not_allowed + "':'';" ); } // Useful in an onChange attribute, eg: //