by Alex Papadimoulis in CodeSOD on Edit

Thomas Nordlander writes: "The Swedish Church of Scientology's scary personality test contains some pretty awesome JavaScript validation. Consider the ingenious way that they make sure they are dealing with numbers."

"I don't think I've ever seen the N-Replace Zero-Test pattern before..."

function validZip(s) { if(trim(document.getElementById("M_land").value.toLowerCase()) != "sverige") { return(true); } s = s.replace(/ /g,""); s = s.replace(/1/g,"0"); s = s.replace(/2/g,"0"); s = s.replace(/3/g,"0"); s = s.replace(/4/g,"0"); s = s.replace(/5/g,"0"); s = s.replace(/6/g,"0"); s = s.replace(/7/g,"0"); s = s.replace(/8/g,"0"); s = s.replace(/9/g,"0"); if(s == "") { return(true); } if(s == "00000") { return(true); } return(false); } function validPhone(s) { s = trim(s); s = s.replace(/ /g,""); s = s.replace(/-/g,""); s = s.replace(/,/g,""); if(s.length < 7) return(false); if(trim(document.getElementById("M_land").value.toLowerCase()) == "sverige") { if(s.substr(0,1) != "0") return(false); if(s.substr(0,2) == "07") return(false); if(s.substr(1,1) < "1" || s.substr(1,1) > "9") return(false); } s = s.replace(/0/g,""); s = s.replace(/1/g,""); s = s.replace(/2/g,""); s = s.replace(/3/g,""); s = s.replace(/4/g,""); s = s.replace(/5/g,""); s = s.replace(/6/g,""); s = s.replace(/7/g,""); s = s.replace(/8/g,""); s = s.replace(/9/g,""); if(s == "") { return(true); } return(false); } function validAge(s) { s = s.replace(/ /g,""); if(s.substr(0,1) == "0") return(false); s = s.replace(/1/g,"0"); s = s.replace(/2/g,"0"); s = s.replace(/3/g,"0"); s = s.replace(/4/g,"0"); s = s.replace(/5/g,"0"); s = s.replace(/6/g,"0"); s = s.replace(/7/g,"0"); s = s.replace(/8/g,"0"); s = s.replace(/9/g,"0"); if(s == "00") { return(true); } return(false); }