// Yeah... I KNOW this is a bit overdone, but what are you // supposed to do all those sunny July afternoons? // 2008-02-18 Lagt till koll på 12-siffrigt personnummer Solvig Grönstedt var otherLocalPartChars = ".-_"; var otherDomainChars = ".-"; var whitespaceChars = " \t\n\r"; var mPrefix = "Du har inte skrivit något i fältet " var mPrefix1 = "Du måste välja något i listan " var mSuffix = ". Det är ett obligatoriskt fält." var ePrefix ="Du skrev ingen giltig e-post adress (t ex foo@bar.se) i fältet " var eSuffix =". Försök igen." var lPrefix = "Innehållet i fältet " var lMidix = " är för långt. Fältet får inte innehålla mer än " var lSuffix = " tecken." var sPrefix = "Innehållet i fältet " var sMidix = " är för litet. Fältet måste minst innehålla " var sSuffix = " tecken." var iSuffix = " måste vara ett tal." function firstChar(s) { return s.charAt(0) } function lastChar(s) { return s.charAt(s.length - 1) } function isEmpty(s) { return ((s == null) || (s.length == 0)) } function isLetter(c) { return (((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z"))) } function isDigit(c) { return ((c >= "0") && (c <= "9")) } function isLetterOrDigit(c) { return (isLetter(c) || isDigit(c)) } function isOtherLocalPartChar(c) { if (otherLocalPartChars.indexOf(c) == -1) return false; else return true; } function isOtherDomainChar(c) { if (otherDomainChars.indexOf(c) == -1) return false; else return true; } function isValidLocalPartChar(c) { return (isLetter(c) || isDigit(c) || isOtherLocalPartChar(c)) } function isValidDomainChar(c) { return (isLetter(c) || isDigit(c) || isOtherDomainChar(c)) } function isWhitespaceChar(c) { if (whitespaceChars.indexOf(c) == -1) return false; else return true; } function indexOfAtSign(s) { var i = s.indexOf("@"); if ((i == s.lastIndexOf("@")) && (i != -1)) return i; else return -1; } function isWhitespace(s) { var i; if (isEmpty(s)) return true; for (i = 0; i < s.length; i++) { var c = s.charAt(i); if (!isWhitespaceChar(c)) return false; } return true; } function isAllValidLocalPartChars(s) { var i; for (i = 0; i < s.length; i++) { var c = s.charAt(i); if (!isValidLocalPartChar(c)) return false; } return true; } function isAllValidDomainChars(s) { var i; for (i = 0; i < s.length; i++) { var c = s.charAt(i); if (!isValidDomainChar(c)) return false; } return true; } function isEmail(s) { var i = indexOfAtSign(s); var sLength = s.length; if (i < 1) return false; if (isWhitespace(s)) return false; var localPart = s.substring(0, i); var domain = s.substring(i + 1, sLength); if (!(isAllValidLocalPartChars(localPart) && isAllValidDomainChars(domain) && isLetterOrDigit(firstChar(localPart)) && isLetterOrDigit(firstChar(domain)) && isLetter(lastChar(domain)) && (domain.indexOf(".") != -1))) return false; return true; } function isTooShort(theField, n) { if (theField.value.length < n) return true; else return false; } function isTooLong(theField, n) { if (theField.value.length > n) return true; else return false; } function isValidInt(myInt) //Funktionen returnerar true om strängen enbart innehåller siffror annars false { if (myInt.length < 1) return (false); if(myInt.charAt(0) == "-") return isPositiveInt(myInt.substring(1,myInt.length)); else return isPositiveInt(myInt.substring(0,myInt.length)); return(true); } function isPositiveInt(myInt) { if (myInt.length < 1) return (false); for (i = 0 ; i < myInt.length ; i++) if (isNaN(parseInt(myInt.charAt(i)))) return (false); return(true); } function isValidYear (myYear) { if (checkSizeInt(myYear, 2000, 2050)) { return(true); } else { return(false); } } function isValidYearBirth (myYear) { if (checkSizeInt(myYear, 1890, 2010)) { return(true); } else { return(false); } } function isValidMonth (myMonth) { if (checkSizeInt(myMonth, 1, 12)){ return(true); } else { return(false); } } function isValidDay (myDay, myMonth, myYear, myName, myField) { var febDays = 28; //todo: kontroller hur man räknar ut skottår if (myYear % 4 == 0) { febDays = 29; } var daysInMonth = new Array(31, febDays, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); if (checkSizeInt(myDay, 1, daysInMonth[myMonth-1])){ return(true); } else { alert (myName + " i " + myField+ " måste vara mellan 01 och " + daysInMonth[myMonth-1]); return(false); } } //************************************************************************** function warnEmpty(theField, s) { theField.value = ""; theField.focus(); alert(mPrefix + s + mSuffix); return false; } function warnEmpty1(theField, s) { theField.value = ""; theField.focus(); alert(mPrefix1 + s); return false; } function warnInt(theField, s) { theField.focus(); theField.select(); alert(lPrefix + s + iSuffix); return false; } function warnInvalid(theField, s) { theField.focus(); theField.select(); alert(s); return false; } function warnInvalidEmail(theField, s) { theField.focus(); theField.select(); alert(ePrefix + s + eSuffix); return false; } function warnTooShort(theField, n, s) { theField.focus alert(sPrefix + s + sMidix + n + sSuffix); return false; } function warnTooLong(theField, n, s) { theField.focus alert(lPrefix + s + lMidix + n + lSuffix); return false; } //************************************************************************** function checkString(theField, s) { if (isWhitespace(theField.value)) return warnEmpty(theField, s); else return true; } function checkInt(theField, s) //2006-04-10 { if (isPositiveInt(theField.value)) return true; else return warnInt(theField, s); } function checkList(theField, s) { if (isWhitespace(theField.value)) return warnEmpty1(theField, s); else return true; } function checkSizeInt(myInt, myMin, myMax) // Kontrollerar myInt att det är en integer och ligger mellan // max och min (inklusive gränsvärdena) { return (isValidInt(myInt)) && (myInt >= myMin) && (myInt <= myMax); } function checkEmail(theField, s) { if (isWhitespace(theField.value)) return warnEmpty(theField, s); else if (!isEmail(theField.value)) return warnInvalidEmail (theField, s); else return true; } function checkTooLong(theField, n, s) { if (isTooLong(theField, n)) return warnTooLong(theField, n, s); else return true; } function checkTooShort(theField, n, s) { if (isTooShort(theField, n)) return warnTooShort(theField, n, s); else return true; } function checkDate(myInput, myName) { if (myInput.value != "") { var val, ar, man, dag; val = myInput.value.toString(); ar = val.substring(0,4); man = val.substring(5,7); dag = val.substring(8,10); if (val.charAt(4) != "-" ) { myInput.focus(); alert(myName + " måste vara i formatet ÅÅÅÅ-MM-DD"); return(false); } if (val.charAt(7) != "-") { myInput.focus(); alert(myName + " måste vara i formatet ÅÅÅÅ-MM-DD"); return(false); } if (!(isValidYear(ar))) { alert("Angivet årtal i " + myName + " är felaktigt") return(false); } if (!(isValidMonth(man))) { alert("Angiven månad i " + myName + " är felaktig") return(false); } if (!(isValidDay(dag,man,ar,"Angiven dag", myName))) { return(false); } return(true); } } function checkYear(myInput, myName) { if (myInput.value != "") { var val, ar; val = myInput.value.toString(); ar = val.substring(0,4); if (!(isValidYear(ar))) { myInput.focus(); alert("Angivet årtal i " + myName + " är felaktigt"); return(false); } return(true); } } function checkYearBirth(myInput, myName) { if (myInput.value != "") { var val, ar; val = myInput.value.toString(); ar = val.substring(0,4); if (!(isValidYearBirth(ar))) { myInput.focus(); alert("Angivet årtal i " + myName + " är felaktigt"); return(false); } return(true); } } //************************************************************************** function checkDateBirth(myInput, myName) { if (myInput.value != "") { var val, ar, man, dag; val = myInput.value.toString(); ar = val.substring(0,4); man = val.substring(5,7); dag = val.substring(8,10); if (val.charAt(4) != "-" ) { myInput.focus(); alert(myName + " måste vara i formatet ÅÅÅÅ-MM-DD"); return(false); } if (val.charAt(7) != "-") { myInput.focus(); alert(myName + " måste vara i formatet ÅÅÅÅ-MM-DD"); return(false); } if (!(isValidYearBirth(ar))) { alert("Angivet årtal i " + myName + " är felaktigt") return(false); } if (!(isValidMonth(man))) { alert("Angiven månad i " + myName + " är felaktig") return(false); } if (!(isValidDay(dag,man,ar,"Angiven dag", myName))) { return(false); } return(true); } } //************************************************************************** function checkDateBirth6(myInput, myName) // Kontrollerar de första 6 tecknen i personnummer { if (myInput.value != "") { var val, ar, man, dag, slask; val = myInput.value.toString(); ar = val.substring(0,2); man = val.substring(2,4); dag = val.substring(4,6); slask = val.substring(6,999); if (checkSizeInt(ar, 0, 99)) { ; } else { alert("Angivet årtal i " + myName + " är felaktigt") return(false); } if (!(isValidMonth(man))) { alert("Angiven månad i " + myName + " är felaktig") return(false); } if (!(isValidDay(dag,man,ar,"Angiven dag", myName))) { return(false); } if (slask.length > 0) { alert("Ange exakt 6 tecken i " + myName) return(false); } return(true); } } //************************************************************************** function checkPersonnr4(myInput) // Kontrollerar att de siste 4 tecknen i personnummer är siffror { if (myInput.value != "") { var val, tal1, tal2, tal3, tal4; val = myInput.value.toString(); tal1 = val.substring(0,1); tal2 = val.substring(1,2); tal3 = val.substring(2,3); tal4 = val.substring(3,4); slask = val.substring(4,999); if (isValidInt(tal1)) { ; } else { alert("Personnummers sista del ska innehålla 4 siffror") return(false); } if (isValidInt(tal2)) { ; } else { alert("Personnummers sista del ska innehålla 4 siffror") return(false); } if (isValidInt(tal3)) { ; } else { alert("Personnummers sista del ska innehålla 4 siffror") return(false); } if (isValidInt(tal4)) { ; } else { alert("Personnummers sista del ska innehålla 4 siffror") return(false); } if (slask.length > 0) { alert("Ange exakt 4 tecken i personnummers sista del") return(false); } return(true); } } //************************************************************************** function validateString(theField, theName, minLen, maxLen, isMan) { if (isMan) { return (checkString(theField, theName) && checkTooShort(theField, minLen, theName) && checkTooLong(theField, maxLen, theName)); } else { if (isWhitespace(theField.value)) return true; else return (checkTooShort(theField, minLen, theName) && checkTooLong(theField, maxLen, theName)); } } function validateInt(theField, theName, isMan) { if (isMan) { return (checkInt(theField, theName)); } else { if (isWhitespace(theField.value)) return true; else return (checkInt(theField, theName)); } } function validateList(theField, theName, isMan) { if (isMan) { return (checkList(theField, theName)); } else { return true; } } function validateDate(theField, theName, isMan) { if (isMan) { return (checkDate(theField, theName)); } else { if (isWhitespace(theField.value)) return true; else return (checkDate(theField, theName)); } } function validateDateBirth(theField, theName, isMan) { if (isMan) { return (checkDateBirth(theField, theName)); } else { if (isWhitespace(theField.value)) return true; else return (checkDateBirth(theField, theName)); } } function validatePersonnr6(theField, theName, isMan) // Kontrollerar de första 6 tecknen i personnummer { if (isMan) { return (checkDateBirth6(theField, theName)); } else { if (isWhitespace(theField.value)) return true; else return (checkDateBirth6(theField, theName)); } } function validatePersonnr4(theField, isMan) // Kontrollerar att de sista 4 tecknen i personnummer är siffror { if (isMan) { return (checkPersonnr4(theField)); } else { if (isWhitespace(theField.value)) return true; else return (checkPersonnr4(theField)); } } function validateYear(theField, theName, isMan) { if (isMan) { return (checkYear(theField, theName)); } else { if (isWhitespace(theField.value)) return true; else return (checkYear(theField, theName)); } } function validateYearBirth(theField, theName, isMan) { if (isMan) { return (checkYearBirth(theField, theName)); } else { if (isWhitespace(theField.value)) return true; else return (checkYearBirth(theField, theName)); } } function validateEmail(theField, theName, isMan) { if (isMan) { return (checkEmail(theField, theName)); } else { if (isWhitespace(theField.value)) return true; else return (checkEmail(theField, theName)); } } function validatePersonnr12(theField, theName, isMan) // Kontrollerar personnummer med 12 siffror { if (isMan) { return (validatePNum(theField.value, theName)); } else { if (isWhitespace(theField.value)) return true; else return (validatePNum(theField.value, theName)); } } function validatePNum(sPNum, theName) { if (sPNum.length != 12) { alert(theName + " ska bestå av 12 siffror."); return false; }; var numbers = sPNum.match(/^(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)$/); var checkSum = 0; if (numbers == null) { alert(theName + " ska endast bestå av siffror."); return false; }; var d = new Date(); // alert(sPNum.substring(0,4)+":"+sPNum.substring(4,6)+":"+sPNum.substring(6,8)); if (!isDate(sPNum.substring(0,4),sPNum.substring(4,6),sPNum.substring(6,8))) { alert(theName + ", datumet " + sPNum.substring(0,8) + " är inte korrekt."); return false; }; // fel i Else: checkSum+=(parseInt(numbers[i])*2)%9 for (var i = 3; i <= 12; i++) { if (i % 2 == 0) { checkSum+=parseInt(numbers[i]); } else { var tmp = parseInt(numbers[i])*2; checkSum += tmp >= 10 ? 1 + tmp % 10 : tmp; } } if (checkSum%10==0) { return true;} alert(theName + " " + sPNum + " är inte korrekt."); return false; } function getYear(y) { return (y < 1000) ? y + 1900 : y; } function isDate(year, month, day) { month = month - 1; // 0-11 in JavaScript var tmpDate = new Date(year,month,day); if ( (getYear(tmpDate.getYear()) == year) && (month == tmpDate.getMonth()) && (day == tmpDate.getDate()) ) return true; else return false; } //**************************************************************************