Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var dictionary = [
- ["select","insert","update","delete","drop","--","alter","xp_","execute","declare","information_schema","table_cursor"], /* the bad words */
- ["select","insert","update","delete","drop","--","alter","xp\_","execute","declare","information_schema","table_cursor"], /* for regexp */
- ["se_lect","in_sert","up_date","de_lete","dr_op","-","al_ter","x.p.","exe_cute","de_clare","infoschema","tablecursor"] /* the replacements */
- ]
- var centerbox = document.getElementById("centerbox");
- var allinputs=centerbox.getElementsByTagName('input'),
- alltextarea=centerbox.getElementsByTagName('textarea');
- jQuery(function($) {
- var symbolsLibrary = [
- ["(",")",";", "#", """],
- ["(",")",";", "#", '"'], // for reg - replace all instances
- ["(", ")", ";", "#", '"']
- ];
- var badSymbol,i,m,temp,
- found = [];
- loopThruFields(allinputs);
- loopThruFields(alltextarea);
- //PHONE FORMAT
- $('.custom_form input[name*=phone]:text').addClass('phonemask');
- $.getScript("common/js/jquery.maskedinput.js",function(){
- $(".phonemask").mask("(999) 999-9999");
- });
- //VALIDATE EMAIL ON BLUR
- /*$('.custom_form input[name*=email]:text').each(function(){
- $(this).blur(function() {
- if(isValidEmailAddress($(this).val())==false){
- inlineMsg(this.id, 'Please enter a valid email address', 0);
- setFocus(this);
- return false;
- }
- });
- });*/
- function setFocus(obj){
- function focusObj(){
- $(obj).focus();
- clearTimeout(t);
- }
- var t = setTimeout(focusObj, 300);
- }
- function loopThruFields(allFields) {
- for (i = 0; i < allFields.length; i++) {
- temp = allFields[i].value;
- for (m=0; m<symbolsLibrary[0].length; m++) {
- badSymbol = symbolsLibrary[0][m];
- if (temp.indexOf(badSymbol) >-1) {
- found.push(badSymbol);
- allFields[i].value = allFields[i].value.replace(new RegExp(symbolsLibrary[0][m], 'g'), symbolsLibrary[2][m]); //Replace ASCII Codes
- }
- } // end m loop
- var currentFocus = allFields[i].getAttribute('onFocus');
- if (allFields[i].type == 'text' || allFields[i].type == 'textarea') {
- if (allFields[i].getAttribute('onFocus') ==null) {
- allFields[i].onfocus = function() {
- wordCheck(this);
- }
- }
- //ZIP NUMBERS ONLY
- if (allFields[i].name.toLowerCase().indexOf("zip") !==-1) {
- allFields[i].onfocus = function() {
- onlyNumbers(this);
- }
- }
- }
- } // end iloop
- } // end loopThruFields
- var submitBtn = document.getElementById("submitButton");
- if (submitBtn) {
- changeInputType(submitBtn, "button")
- }
- });
- function changeInputType(oldObject, oType) {
- var newObject = document.createElement('input');
- newObject.onclick = function() {
- validateForm();
- }
- newObject.type = oType;
- // 'Copy' old types to new element
- if(oldObject.size)
- newObject.size = oldObject.size;
- if(oldObject.value)
- newObject.value = oldObject.value;
- if(oldObject.name)
- newObject.name = oldObject.name;
- if(oldObject.id)
- newObject.id = oldObject.id;
- if(oldObject.className)
- newObject.className = oldObject.className;
- // replace old object with new one
- oldObject.parentNode.replaceChild(newObject,oldObject);
- return newObject;
- }
- // ################ START VALIDATION ######################### \\
- // validates elements whose ID starts with "req_".
- function validateForm() {
- var myArray = new Array(),
- myTags=document.getElementsByTagName('*');
- var isvalid = true;
- for (var s=0; s<myTags.length; s++) {
- if (myTags[s].id.match('req_') != null) {
- var geta = document.getElementById(myTags[s].id);
- myArray[s] = geta;
- }
- }
- loopThruFields2(allinputs);
- loopThruFields2(alltextarea);
- for (var i in myArray) {
- if (myArray[i].value == ""){
- inlineMsg(myArray[i].id,'<strong>Required</strong>',2);
- return false;
- }
- }
- $('.custom_form input[name*=email]:text').each(function(){ //VALIDATE EMAIL
- $(this).val($(this).val().replace(/\s/g, ""));
- //alert(emailTrim);
- if(isValidEmailAddress($(this).val())==false) { // if not true show validation
- inlineMsg(this.id, 'Please enter a valid email address', 2);
- isvalid = false;
- }
- else
- isvalid = true;
- });
- if(isvalid)
- //document.custom_form.submit();
- alert('submit');
- }
- function loopThruFields2(allFields) {
- var temp, badSymbol, found = [];
- for (var j = 0; j < allFields.length; j++) {
- temp = allFields[j].value;
- for (var m=0; m<dictionary[0].length; m++) {
- badSymbol = dictionary[0][m];
- if (temp.indexOf(badSymbol) >-1) {
- found.push(badSymbol);
- allFields[j].value = allFields[j].value.replace(new RegExp(dictionary[0][m], 'g'), dictionary[2][m]); //Replace ASCII Codes
- }
- }
- }
- }
- // ################ START TEXT LENGTH LIMITS ######################### \\
- function limitText(formField, messageDiv, limitNum) {
- var countDownId = document.getElementById(messageDiv);
- countDownId.style.display = "inline";
- countDownId.innerHTML = limitNum - formField.value.length + " characters left.";
- formField.onblur=function() {
- countDownId.style.display = "none";
- };
- formField.onkeydown= function() {countdownEvent(); };
- formField.onkeyup= function() {countdownEvent(); };
- function countdownEvent() {
- if (formField.value.length > limitNum) {
- formField.value = formField.value.substring(0, limitNum);
- } else {
- countDownId.innerHTML = limitNum - formField.value.length + " characters left.";
- }
- replaceFromDictionary(formField);
- };
- replaceFromDictionary(formField);
- }
- // ################ SQL WORDS VALIDATION ######################### \\
- function wordCheck(formField) {
- formField.onkeydown= function() {listenEvent(); };
- formField.onkeyup= function() {listenEvent(); };
- function listenEvent() {
- replaceFromDictionary(formField);
- }
- replaceFromDictionary(formField);
- }
- function replaceFromDictionary(formfield) {
- var fixed = formfield.value,
- lcVal = formfield.value.toLowerCase(),
- i = dictionary[0].length,
- bad = '',
- reg = new RegExp();
- found = [];
- for (i = 0; i < dictionary[0].length; i++) {
- var replaceBad, matchBad, badLowCase, pos, badCaseLower, newFixed = '';
- bad = dictionary[0][i];
- var temp = lcVal;
- if (temp.indexOf(bad)>-1) { //lowercase word exists in library
- pos= temp.indexOf(bad);
- temp = fixed.substring(pos, (pos + bad.length)); //get bad value
- badCaseLower = temp.toLowerCase();
- newFixed = fixed.substr(0, pos) + badCaseLower; // = field value + lower case of bad value
- }
- if (newFixed.indexOf(bad) > -1) {
- found.push(bad);
- fixed = newFixed.replace(new RegExp(dictionary[1][i], 'g'), dictionary[2][i]);
- }
- }
- if (found.length > 0) {
- formfield.value = fixed;
- failMsg(formfield, found);
- }
- }
- function failMsg(formField, badwords) {
- if(formField.id == "") {
- var newDate = new Date();
- formField.id = "newId"+ newDate.getTime();
- }
- var multiple, bad = '';
- if (badwords.length == 1) {
- multiple = false;
- bad = badwords[0];
- } else {
- multiple = true;
- badwords[badwords.length-1] = 'and ' + badwords[badwords.length-1];
- bad = badwords.join(', ');
- }
- inlineMsg(formField.id, 'We\'re sorry, we have found the word' + (multiple ? 's' : '') +' <b>'+bad+'</b> in this field. ' + (multiple ? 'These words are' : 'This word is') + ' not allowed in our webform, per Congressional regulations. We have chosen alternate words to use in your submission; please review the changes in the form before you submit it.',7);
- }
- // ################ LIMIT CHARACTERS TO NUMBERS ONLY ########### //
- function onlyNumbers(objectName){
- validateNums(objectName);
- objectName.onkeydown= function() {validateNums(objectName);};
- objectName.onkeyup= function() {validateNums(objectName);};
- }
- function validateNums(objectName){
- var checkOK = "0123456789 -)(",
- checkStr = objectName,
- allValid = true,
- decPoints = 0,
- allNum = "",
- ch, i;
- for (i = 0; i < checkStr.value.length; i++) {
- ch = checkStr.value.charAt(i);
- for (var j = 0; j < checkOK.length; j++)
- if (ch == checkOK.charAt(j)) break;
- if (j == checkOK.length) {
- allValid = false;
- break;
- }
- if (ch != ",") allNum += ch;
- }
- if (!allValid) {
- var str = checkStr.value;
- objectName.value = str.replace(ch, "");
- inlineMsg(objectName.id, 'Character "<b>'+ch+'</b>" is not allowed. Only numbers are allowed for this field', 0)
- //return (false);
- } else {
- //hideMessage();
- }
- }
- // ################ EMAIL ADDRESS FORMAT VALIDATION ##################//
- function isValidEmailAddress(emailAddress) { // validate email and return true if valid
- var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
- return pattern.test(emailAddress);
- }
- function hideMessage() {
- var errMsg = document.getElementById('errMsg');
- if(errMsg)
- errMsg.style.display = 'none';
- }
- // ################ inlineMsg BOX ################ //
- var MSGTIMER = 20,
- MSGSPEED = 5,
- MSGOFFSET = 3,
- MSGHIDE = 3;
- // build out the divs, set attributes and call the fade function //
- function inlineMsg(target,string,autohide) {
- var msg;
- var msgcontent;
- if(!document.getElementById('errMsg')) {
- msg = document.createElement('div');
- msg.id = 'errMsg';
- msgcontent = document.createElement('div');
- msgcontent.id = 'msgcontent';
- document.body.appendChild(msg);
- msg.appendChild(msgcontent);
- msg.style.filter = 'alpha(opacity=0)';
- msg.style.opacity = 0;
- msg.alpha = 0;
- } else {
- msg = document.getElementById('errMsg');
- msgcontent = document.getElementById('msgcontent');
- }
- msgcontent.innerHTML = string;
- msg.style.position = 'absolute';
- msg.style.zIndex = 200;
- msg.style.display = 'block';
- msgcontent.style.display = 'block';
- var msgheight = msg.offsetHeight;
- /*var targetdiv = document.getElementById(target);*/
- var targetdiv = $('#'+target);
- $('#'+target).focus();
- var targetheight = targetdiv.height();//targetdiv.offsetHeight;
- var targetwidth = targetdiv.width();//targetdiv.offsetWidth;
- var topposition = targetdiv.offset().top //topPosition(targetdiv) - ((msgheight - targetheight) / 2);
- topposition = topposition - ((msgheight - targetheight) / 2);
- var leftposition = targetdiv.offset().left + targetwidth + MSGOFFSET ; //leftPosition(targetdiv) + targetwidth + MSGOFFSET;
- msg.style.top = topposition + 'px';
- msg.style.left = leftposition + 'px';
- clearInterval(msg.timer);
- msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
- if(!autohide) {
- autohide = MSGHIDE;
- }
- window.setTimeout("hideMsg()", (autohide * 1000));
- }
- // hide the form alert //
- function hideMsg(msg) {
- var msg = document.getElementById('errMsg');
- if(!msg.timer) {
- msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
- }
- }
- // face the message box //
- function fadeMsg(flag) {
- if(flag == null) {
- flag = 1;
- }
- var msg = document.getElementById('errMsg');
- var value;
- if(flag == 1) {
- value = msg.alpha + MSGSPEED;
- } else {
- value = msg.alpha - MSGSPEED;
- }
- msg.alpha = value;
- msg.style.opacity = (value / 100);
- msg.style.filter = 'alpha(opacity=' + value + ')';
- if(value >= 99) {
- clearInterval(msg.timer);
- msg.timer = null;
- } else if(value <= 1) {
- msg.style.display = "none";
- clearInterval(msg.timer);
- }
- }
- // calculate the position of the element in relation to the left of the browser //
- function leftPosition(target) {
- var left = 0;
- if(target.offsetParent) {
- while(1) {
- left += target.offsetLeft;
- if(!target.offsetParent) {
- break;
- }
- target = target.offsetParent;
- }
- } else if(target.x) {
- left += target.x;
- }
- return left;
- }
- // calculate the position of the element in relation to the top of the browser window //
- function topPosition(target) {
- var top = 0;
- if(target.offsetParent) {
- while(1) {
- top += target.offsetTop;
- if(!target.offsetParent) {
- break;
- }
- target = target.offsetParent;
- }
- } else if(target.y) {
- top += target.y;
- }
- return top;
- }
- // preload the arrow //
- if(document.images) {
- arrow = new Image(7,80);
- arrow.src = "common/images/msg_arrow.gif";
- }
Add Comment
Please, Sign In to add comment