/** * Validates that input's value is correct email address */ function validateEmail(elem) { var str = ""; if (elem.value) { str = new String(elem.value); } else { str = new String(elem); } if (window.RegExp) { var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)"; var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,4})(\\]?)$"; var reg3str = "\"([^\"]+)\"\\s+<{1}([A-z]{1}[A-z0-9\\x2d\\.]*[A-z0-9]{1}@[A-z0-9\\x2d\\.]*[A-z-9]{1}\\.[A-z]{2,5})>{1}"; var reg1 = new RegExp(reg1str); var reg2 = new RegExp(reg2str); var reg3 = new RegExp(reg3str); if (!reg1.test(str) && (reg2.test(str) || reg3.test(str))) return true; return false; } else { if (str.indexOf("@") >= 0) return true; return false; } } /** * Returns value of selected option in listbox * If there is no options selected - returns null. * If multiple options selected - returns first. */ function selected(sel){ if(!sel.options){ return null; } for(var i = 0; i< sel.options.length; i++){ if(sel.options[i].selected){ return sel.options[i].value; } } return null; } /** * Returns value of checked radiobutton in radiobuttons group * If there is no radiobuttons checked - returns null. */ function selectedRadio(sel){ if(!sel){ return null; } if(!sel.length){ if(!sel.checked){ return null; } return true; } for(var i = 0; i< sel.length; i++){ if(sel[i].checked){ return sel[i].value; } } return null; } /** * Returns true if one of options in radiogroup is checked; */ function isChecked(sel){ return selectedRadio(sel) != null; } String.prototype.trim = function() { return this.replace(/(^\s*)|(\s*$)/g, ""); } /** * returns string with all leading and trailing characters * eliminated. */ function trim(str){ var s = new String(str); //trailing spaces while (s.length>0 && isSpaceCharacter(""+s.charAt(s.length-1))){ s = s.substring(0,s.length-1); } //leading spaces while (s.length>0 && isSpaceCharacter(""+s.charAt(0))){ s = s.substring(1); } return s } var spaces = " \t\r\n"+String.fromCharCode(160); function isSpaceCharacter(ch){ return spaces.indexOf(ch) >-1; } function isEmpty(elem){ return trim(elem.value).length==0; } function validateForm(){ if(isEmpty(f.name)){ alert("Please enter name"); f.name.focus(); return false; } if(isEmpty(f.email) || !validateEmail(f.email)){ alert("Please enter valid email address"); f.email.focus(); return false; } if(isEmpty(f.phone)){ alert("Please enter phone"); f.phone.focus(); return false; } if(isEmpty(f.contactmessage)){ alert("Please enter sending message"); f.contactmessage.focus(); return false; } f.submit(); }