backoffice/html/nonimg/validate.js
2011-06-21 13:28:10 +00:00

796 lines
24 KiB
JavaScript

function Trim(s) {
var temp = " ";
var i = 0;
while ((temp == " ") && (i <= s.length)) {
temp = s.charAt(i);
i++;
}
s = s.substring(i - 1, s.length);
return(s);
}
function replaceAll(str, from, to) {
var idx;
idx = -1;
if ((str != null) && (str != "")) {idx = str.indexOf(from);}
while (idx > -1) {
str = str.replace(from, to);
idx = str.indexOf(from);
}
return str;
}
function RequiredField(val) {
var value = Trim(val);
if(value=="") {
return false;
}
return true;
}
function EmailValidate(val) {
if(val==""){
return true;
//Can be return false; if you want to validate "cannot be blank value"
}
var value = Trim(val);
var emailPat=/^(.+)@(.+)$/;
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
var validChars="\[^\\s" + specialChars + "\]";
var quotedUser="(\"[^\"]*\")";
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
var atom=validChars + '+';
var word="(" + atom + "|" + quotedUser + ")";
var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
var matchArray=value.match(emailPat);
if (matchArray==null) {
return false;
}
var user=matchArray[1];
var domain=matchArray[2];
if (user.match(userPat)==null){
return false;
}
return true;
}
function RangeNumber(val,op,std1,std2){
if(val==""){
return true ;
}
if((op=="=") && (val==std1)){
return true ;
}
if((op==">=") && (val>=std1)){
return true ;
}
if((op==">") && (val>std1)){
return true ;
}
if((op=="<=") && (val<=std1)){
return true ;
}
if((op=="<") && (val<std1)){
return true ;
}
if((op=="!=") && (val!=std1)){
return true ;
}
if((op=="><") && (val>=std1)&& (val<=std2)){
return true ;
}
return false ;
}
function NumberValidate(val) {
if(val==""){
return true;
}
var value = val;
value = replaceAll(value, ',','');
if (!isFinite(value)) {
return false;
}
return true;
}
function Trim(s){
var temp = " ";
var i = 0;
while ((temp == " ") && (i <= s.length)) {
temp = s.charAt(i);
i++;
}
s = s.substring(i - 1, s.length);
return(s);
}
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;
function isInteger(s){
var i;
for (i = 0; i < s.length; i++){
// Check that current character is number.
var c = s.charAt(i);
if (((c < "0") || (c > "9"))) return false;
}
// All characters are numbers.
return true;
}
function stripCharsInBag(s, bag){
var i;
var returnString = "";
// Search through string's characters one by one.
// If character is not in bag, append to returnString.
for (i = 0; i < s.length; i++){
var c = s.charAt(i);
if (bag.indexOf(c) == -1) returnString += c;
}
return returnString;
}
function daysInFebruary (year){
// February has 29 days in any year evenly divisible by four,
// EXCEPT for centurial years which are not also divisible by 400.
return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
for (var i = 1; i <= n; i++) {
this[i] = 31
if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
if (i==2) {this[i] = 29}
}
return this
}
function isDate(dtStr){
var daysInMonth = DaysArray(12)
var pos1=dtStr.indexOf(dtCh)
var pos2=dtStr.indexOf(dtCh,pos1+1)
var strDay=dtStr.substring(0,pos1)
var strMonth=dtStr.substring(pos1+1,pos2)
var strYear=dtStr.substring(pos2+1)
strYr=strYear
if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
for (var i = 1; i <= 3; i++) {
if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
}
month=parseInt(strMonth)
day=parseInt(strDay)
year=parseInt(strYr)
if (pos1==-1 || pos2==-1){
//alert("The date format should be : mm/dd/yyyy")
return false
}
if (strMonth.length<1 || month<1 || month>12){
//alert("Please enter a valid month")
return false
}
if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
//alert("Please enter a valid day")
return false
}
if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
return false
}
if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
//alert("Please enter a valid date")
return false
}
return true
}
function IntValidate(val) {
if(val==""){
return true;
}
var value = val;
value = replaceAll(value, ',','');
if (!isFinite(value)) {
return false;
}
value = value.split('.').length-1 ;
if (value>0) {
return false;
}
return true;
}
function LengthStr(val,op,size1,size2) {
if(val==""){
return true;
}
var value = Trim(val);
if((op==">") && (value.length>size1)) {
return true;
}
if((op==">=") && (value.length>=size1)) {
return true;
}
if((op=="<") && (value.length<size1)) {
return true;
}
if((op=="<=") && (value.length<=size1)) {
return true;
}
if((op=="=") && (value.length==size1)) {
return true;
}
if((op=="!=") && (value.length!=size1)) {
return true;
}
if((op=="><") && (value.length>=size1) && (value.length<=size2)) {
return true;
}
return false;
}
/*
// -------------------------------------------------------------------
// hasOptions(obj)
// Utility function to determine if a select object has an options array
// -------------------------------------------------------------------
function hasOptions(obj) {
if (obj!=null && obj.options!=null) { return true; }
return false;
}
// -------------------------------------------------------------------
// selectUnselectMatchingOptions(select_object,regex,select/unselect,true/false)
// This is a general function used by the select functions below, to
// avoid code duplication
// -------------------------------------------------------------------
function selectUnselectMatchingOptions(obj,regex,which,only) {
if (window.RegExp) {
if (which == "select") {
var selected1=true;
var selected2=false;
}
else if (which == "unselect") {
var selected1=false;
var selected2=true;
}
else {
return;
}
var re = new RegExp(regex);
if (!hasOptions(obj)) { return; }
for (var i=0; i<obj.options.length; i++) {
if (re.test(obj.options[i].text)) {
obj.options[i].selected = selected1;
}
else {
if (only == true) {
obj.options[i].selected = selected2;
}
}
}
}
}
// -------------------------------------------------------------------
// selectMatchingOptions(select_object,regex)
// This function selects all options that match the regular expression
// passed in. Currently-selected options will not be changed.
// -------------------------------------------------------------------
function selectMatchingOptions(obj,regex) {
selectUnselectMatchingOptions(obj,regex,"select",false);
}
// -------------------------------------------------------------------
// selectOnlyMatchingOptions(select_object,regex)
// This function selects all options that match the regular expression
// passed in. Selected options that don't match will be un-selected.
// -------------------------------------------------------------------
function selectOnlyMatchingOptions(obj,regex) {
selectUnselectMatchingOptions(obj,regex,"select",true);
}
// -------------------------------------------------------------------
// unSelectMatchingOptions(select_object,regex)
// This function Unselects all options that match the regular expression
// passed in.
// -------------------------------------------------------------------
function unSelectMatchingOptions(obj,regex) {
selectUnselectMatchingOptions(obj,regex,"unselect",false);
}
// -------------------------------------------------------------------
// sortSelect(select_object)
// Pass this function a SELECT object and the options will be sorted
// by their text (display) values
// -------------------------------------------------------------------
function sortSelect(obj) {
var o = new Array();
if (!hasOptions(obj)) { return; }
for (var i=0; i<obj.options.length; i++) {
o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected) ;
}
if (o.length==0) { return; }
o = o.sort(
function(a,b) {
if ((a.text+"") < (b.text+"")) { return -1; }
if ((a.text+"") > (b.text+"")) { return 1; }
return 0;
}
);
for (var i=0; i<o.length; i++) {
obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
}
}
// -------------------------------------------------------------------
// selectAllOptions(select_object)
// This function takes a select box and selects all options (in a
// multiple select object). This is used when passing values between
// two select boxes. Select all options in the right box before
// submitting the form so the values will be sent to the server.
// -------------------------------------------------------------------
function selectAllOptions(obj) {
if (!hasOptions(obj)) { return; }
for (var i=0; i<obj.options.length; i++) {
obj.options[i].selected = true;
}
}
// -------------------------------------------------------------------
// moveSelectedOptions(select_object,select_object[,autosort(true/false)[,regex]])
// This function moves options between select boxes. Works best with
// multi-select boxes to create the common Windows control effect.
// Passes all selected values from the first object to the second
// object and re-sorts each box.
// If a third argument of 'false' is passed, then the lists are not
// sorted after the move.
// If a fourth string argument is passed, this will function as a
// Regular Expression to match against the TEXT or the options. If
// the text of an option matches the pattern, it will NOT be moved.
// It will be treated as an unmoveable option.
// You can also put this into the <SELECT> object as follows:
// onDblClick="moveSelectedOptions(this,this.form.target)
// This way, when the user double-clicks on a value in one box, it
// will be transferred to the other (in browsers that support the
// onDblClick() event handler).
// -------------------------------------------------------------------
function moveSelectedOptions(from,to) {
// Unselect matching options, if required
if (arguments.length>3) {
var regex = arguments[3];
if (regex != "") {
unSelectMatchingOptions(from,regex);
}
}
// Move them over
if (!hasOptions(from)) { return; }
for (var i=0; i<from.options.length; i++) {
var o = from.options[i];
if (o.selected) {
if (!hasOptions(to)) { var index = 0; } else { var index=to.options.length; }
to.options[index] = new Option( o.text, o.value, false, false);
}
}
// Delete them from original
for (var i=(from.options.length-1); i>=0; i--) {
var o = from.options[i];
if (o.selected) {
from.options[i] = null;
}
}
if ((arguments.length<3) || (arguments[2]==true)) {
sortSelect(from);
sortSelect(to);
}
from.selectedIndex = -1;
to.selectedIndex = -1;
}
// -------------------------------------------------------------------
// copySelectedOptions(select_object,select_object[,autosort(true/false)])
// This function copies options between select boxes instead of
// moving items. Duplicates in the target list are not allowed.
// -------------------------------------------------------------------
function copySelectedOptions(from,to) {
var options = new Object();
if (hasOptions(to)) {
for (var i=0; i<to.options.length; i++) {
options[to.options[i].value] = to.options[i].text;
}
}
if (!hasOptions(from)) { return; }
for (var i=0; i<from.options.length; i++) {
var o = from.options[i];
if (o.selected) {
if (options[o.value] == null || options[o.value] == "undefined" || options[o.value]!=o.text) {
if (!hasOptions(to)) { var index = 0; } else { var index=to.options.length; }
to.options[index] = new Option( o.text, o.value, false, false);
}
}
}
if ((arguments.length<3) || (arguments[2]==true)) {
sortSelect(to);
}
from.selectedIndex = -1;
to.selectedIndex = -1;
}
// -------------------------------------------------------------------
// moveAllOptions(select_object,select_object[,autosort(true/false)[,regex]])
// Move all options from one select box to another.
// -------------------------------------------------------------------
function moveAllOptions(from,to) {
selectAllOptions(from);
if (arguments.length==2) {
moveSelectedOptions(from,to);
}
else if (arguments.length==3) {
moveSelectedOptions(from,to,arguments[2]);
}
else if (arguments.length==4) {
moveSelectedOptions(from,to,arguments[2],arguments[3]);
}
}
// -------------------------------------------------------------------
// copyAllOptions(select_object,select_object[,autosort(true/false)])
// Copy all options from one select box to another, instead of
// removing items. Duplicates in the target list are not allowed.
// -------------------------------------------------------------------
function copyAllOptions(from,to) {
selectAllOptions(from);
if (arguments.length==2) {
copySelectedOptions(from,to);
}
else if (arguments.length==3) {
copySelectedOptions(from,to,arguments[2]);
}
}
// -------------------------------------------------------------------
// swapOptions(select_object,option1,option2)
// Swap positions of two options in a select list
// -------------------------------------------------------------------
function swapOptions(obj,i,j) {
var o = obj.options;
var i_selected = o[i].selected;
var j_selected = o[j].selected;
var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
var temp2= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);
o[i] = temp2;
o[j] = temp;
o[i].selected = j_selected;
o[j].selected = i_selected;
}
// -------------------------------------------------------------------
// moveOptionUp(select_object)
// Move selected option in a select list up one
// -------------------------------------------------------------------
function moveOptionUp(obj) {
if (!hasOptions(obj)) { return; }
for (i=0; i<obj.options.length; i++) {
if (obj.options[i].selected) {
if (i != 0 && !obj.options[i-1].selected) {
swapOptions(obj,i,i-1);
obj.options[i-1].selected = true;
}
}
}
}
// -------------------------------------------------------------------
// moveOptionDown(select_object)
// Move selected option in a select list down one
// -------------------------------------------------------------------
function moveOptionDown(obj) {
if (!hasOptions(obj)) { return; }
for (i=obj.options.length-1; i>=0; i--) {
if (obj.options[i].selected) {
if (i != (obj.options.length-1) && ! obj.options[i+1].selected) {
swapOptions(obj,i,i+1);
obj.options[i+1].selected = true;
}
}
}
}
// -------------------------------------------------------------------
// removeSelectedOptions(select_object)
// Remove all selected options from a list
// (Thanks to Gene Ninestein)
// -------------------------------------------------------------------
function removeSelectedOptions(from) {
if (!hasOptions(from)) { return; }
if (from.type=="select-one") {
from.options[from.selectedIndex] = null;
}
else {
for (var i=(from.options.length-1); i>=0; i--) {
var o=from.options[i];
if (o.selected) {
from.options[i] = null;
}
}
}
from.selectedIndex = -1;
}
// -------------------------------------------------------------------
// removeAllOptions(select_object)
// Remove all options from a list
// -------------------------------------------------------------------
function removeAllOptions(from) {
if (!hasOptions(from)) { return; }
for (var i=(from.options.length-1); i>=0; i--) {
from.options[i] = null;
}
from.selectedIndex = -1;
}
// -------------------------------------------------------------------
// addOption(select_object,display_text,value,selected)
// Add an option to a list
// -------------------------------------------------------------------
function addOption(obj,text,value,selected) {
if (obj!=null && obj.options!=null) {
obj.options[obj.options.length] = new Option(text, value, false, selected);
}
}*/
/**
* Copyright (c)2005-2007 Matt Kruse (javascripttoolbox.com)
*
* Dual licensed under the MIT and GPL licenses.
* This basically means you can use this code however you want for
* free, but don't claim to have written it yourself!
* Donations always accepted: http://www.JavascriptToolbox.com/donate/
*
* Please do not link to the .js files on javascripttoolbox.com from
* your site. Copy the files locally to your server instead.
*
*/
var Selectbox = new Object();
Selectbox.$VERSION = 1.0;
Selectbox.hasOptions = function(obj) {
return (obj!=null && typeof(obj.options)!="undefined" && obj.options!=null);
};
Selectbox.selectUnselectMatchingOptions = function(obj,regex,which,only) {
if (window.RegExp) {
if (!this.hasOptions(obj)) { return false; }
if (typeof(only)=="undefined" || only==null) { var only = false; }
var re = new RegExp(regex);
for (var i=0; i<obj.options.length; i++) {
if (re.test(obj.options[i].text)) {
obj.options[i].selected = (which=="select");
}
else if (only) {
obj.options[i].selected = (which=="unselect");
}
}
return true;
}
return false;
};
Selectbox.selectOptions = function(obj,regex) {
return this.selectUnselectMatchingOptions(obj,regex,"select",false);
};
Selectbox.selectOnlyOptions = function(obj,regex) {
return this.selectUnselectMatchingOptions(obj,regex,"select",true);
};
Selectbox.unselectOptions = function(obj,regex) {
return this.selectUnselectMatchingOptions(obj,regex,"unselect",false);
};
Selectbox.sort = function(obj) {
var o = new Array();
if (!this.hasOptions(obj)) { return false; }
for (var i=0; i<obj.options.length; i++) {
o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected) ;
}
if (o.length==0) { return true; }
o = o.sort(
function(a,b) {
if ((a.text+"") < (b.text+"")) { return -1; }
if ((a.text+"") > (b.text+"")) { return 1; }
return 0;
}
);
for (var i=0; i<o.length; i++) {
obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
}
return true;
};
Selectbox.selectAllOptions = function(obj) {
if (!this.hasOptions(obj)) { return false; }
for (var i=0; i<obj.options.length; i++) {
obj.options[i].selected = true;
}
return true;
};
Selectbox.moveSelectedOptions = function(from,to) {
if (!this.hasOptions(from)) { return false; }
// Unselect matching options, if required
if (arguments.length>3) {
var regex = arguments[3];
if (regex != "") {
if (!this.unselectOptions(from,regex)) {
return false;
}
}
}
// Move them over
for (var i=0; i<from.options.length; i++) {
var o = from.options[i];
if (o.selected) {
if (!this.hasOptions(to)) {
var index = 0;
}
else {
var index=to.options.length;
}
to.options[index] = new Option(o.text, o.value, false, false);
}
}
// Delete them from original
for (var i=(from.options.length-1); i>=0; i--) {
var o = from.options[i];
if (o.selected) {
from.options[i] = null;
}
}
if ((arguments.length<3) || (arguments[2])) {
this.sort(from);
this.sort(to);
}
from.selectedIndex = -1;
to.selectedIndex = -1;
return true;
};
Selectbox.copySelectedOptions = function(from,to) {
if (!this.hasOptions(from)) { return false; }
var options = new Object();
if (this.hasOptions(to)) {
for (var i=0; i<to.options.length; i++) {
options[to.options[i].value] = to.options[i].text;
}
}
for (var i=0; i<from.options.length; i++) {
var o = from.options[i];
if (o.selected) {
if (typeof(options[o.value])=="undefined" || options[o.value]==null || options[o.value]!=o.text) {
if (!this.hasOptions(to)) {
var index = 0;
}
else {
var index=to.options.length;
}
to.options[index] = new Option( o.text, o.value, false, false);
}
}
}
if ((arguments.length<3) || (arguments[2]==true)) {
this.sort(to);
}
from.selectedIndex = -1;
to.selectedIndex = -1;
return true;
};
Selectbox.moveAllOptions = function(from,to) {
this.selectAllOptions(from);
if (arguments.length==2) {
this.moveSelectedOptions(from,to);
}
else if (arguments.length==3) {
this.moveSelectedOptions(from,to,arguments[2]);
}
else if (arguments.length==4) {
this.moveSelectedOptions(from,to,arguments[2],arguments[3]);
}
};
Selectbox.copyAllOptions = function(from,to) {
this.selectAllOptions(from);
if (arguments.length==2) {
this.copySelectedOptions(from,to);
}
else if (arguments.length==3) {
this.copySelectedOptions(from,to,arguments[2]);
}
};
Selectbox.swapOptions = function(obj,i,j) {
if (!this.hasOptions(obj)) { return false; }
var o = obj.options;
if (i<0 || i>=o.length || j<0 || j>=o.length) { return false; }
var i_selected = o[i].selected;
var j_selected = o[j].selected;
var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
var temp2= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);
o[i] = temp2;
o[j] = temp;
o[i].selected = j_selected;
o[j].selected = i_selected;
return true;
};
Selectbox.moveOptionUp = function(obj) {
if (!this.hasOptions(obj)) { return false; }
for (i=0; i<obj.options.length; i++) {
if (obj.options[i].selected) {
if (i>0 && !obj.options[i-1].selected) {
this.swapOptions(obj,i,i-1);
obj.options[i-1].selected = true;
}
}
}
return true;
};
Selectbox.moveOptionDown = function(obj) {
if (!this.hasOptions(obj)) { return false; }
for (i=obj.options.length-1; i>=0; i--) {
if (obj.options[i].selected) {
if (i != (obj.options.length-1) && ! obj.options[i+1].selected) {
this.swapOptions(obj,i,i+1);
obj.options[i+1].selected = true;
}
}
}
return true;
};
Selectbox.removeSelectedOptions = function(from) {
if (!this.hasOptions(from)) { return false; }
if (from.type=="select-one" && from.selectedIndex>=0) {
from.options[from.selectedIndex] = null;
}
else {
for (var i=(from.options.length-1); i>=0; i--) {
var o=from.options[i];
if (o.selected) {
from.options[i] = null;
}
}
}
from.selectedIndex = -1;
};
Selectbox.removeAllOptions = function(from) {
if (!this.hasOptions(from)) { return false; }
for (var i=(from.options.length-1); i>=0; i--) {
from.options[i] = null;
}
from.selectedIndex = -1;
return true;
};
Selectbox.addOption = function(obj,text,value,selected) {
if (obj!=null && obj.options!=null) {
obj.options[obj.options.length] = new Option(text, value, false, selected);
}
};
Selectbox.hasOneOption = function(obj) {
if (!this.hasOptions(obj)) { return false; }
if (obj.options.length==1) { return true; }
return false;
};