Czesc, w szkole mielismy do napisania formularz JSowy. Wiekszosc robilo mnostwo ifow, strasznie mi sie to nie podobalo, wiec postanowilem, ze zrobie to inaczej i wyszlo takie cos.
Nie ma tutaj htmla, ale mysle ze chyba nie jest on potrzebny. Co o tym myslicie?
function check() {
this.valide_arr = [
{ element:"imie",regex:"^[a-zA-ZąćęłśóźżńĄĘĆŁŚÓŃŻŹ]{3,20}$"},
{ element:"nazwisko",regex:"^[a-zA-ZąćęłśóźżńĄĘĆŁŚÓŃŻŹ]{5,20}$"},
{ element:"miejscowosc",regex:"^[' 'a-zA-ZąćęłśóźżńĄĘĆŁŚÓŃŻŹ]{3,25}$"},
{ element:"adres",regex:"^[a-zA-ZąćęłśóźżńĄĘĆŁŚÓŃŻŹ]+\ [a-zA-Z0-9\ \/]*$"},
{ element:"email",regex:"^[a-z0-9\._%-]+@[a-z0-9\.-]+\.[a-z]{2,4}$"},
{ element:"telefon",regex:"^[-0-9]{11}$"},
{ element:"kodpocztowy",regex:"^[0-9]{2}\-[0-9]{3}$"},
{ element:"opinia",regex:"^[' 'a-zA-Z0-9=ąćęłśóźżńĄĘĆŁŚÓŃŻŹ,.?:!]{20,200}$"}
];
this.test = function (input) {
var name_input = input.name;
this.valide_arr.map(function(value,key){
if(name_input == value.element){
if(input.value == ""){
input.style.backgroundColor = "white";
}
else if(input.value.match(new RegExp(value.regex))==null) {
input.style.backgroundColor = "#DF514C";
}
else {
input.style.backgroundColor = "#7DD34F";
}
};
});
};
this.refresh = function () {
location.reload();
}
this.test2 = function (e,form) {
var wynik =[];
var bad = 0;
var good = 0;
var input = document.querySelectorAll('[required]');
for(i=0; i<input.length; i++){
wynik.push(input[i].value.match(new RegExp(this.valide_arr[i].regex)));
wynik[i] == null ? bad++ : good++;
};
good==input.length && good>1 ? console.log("ok") : e.preventDefault();
};
};
var fo = new check();