Artykuły
Ilość wyświetleń: 2138 « powrót
<FORM NAME="form1"> Wiek: <INPUT TYPE="text" NAME="nazwa1" VALUE="0"> Identyfikator: <INPUT TYPE="text" NAME="nazwa2"> Data [dd/mm/rrrr]: <INPUT TYPE="text" NAME="nazwa3"> <INPUT TYPE="submit" NAME="ok" VALUE="Wyślij"> </FORM>
<FORM NAME="form1" onSubmit="return Validate();">
<script language="JavaScript" src="tvalidation.js">
</script>
<script>
function Validate()
{
var v = new TValidation("form1");
v.AddItem("nazwa1", "Wiek musi byc wartością liczbową o max . długości 3 znaków", "vLength(v, 3) && Integer(v)");
v.AddItem("nazwa2", "Pole musi być wypełnione i nie może być dłuższe niż 12 znaków", "vLength(v, 12) && vRequired(v)");
v.AddItem("nazwa3", "Data musi być w formacie dd/mm/rrrr", "vDate(v)");
return v.Check();
}
</script>
AddItem(nazwa_pola, komunikat_w_wypadku_bledu, regula_poprawnosci)
vLength(val, nLen)
vRequired(val)
vIsInteger(val)
vIsFloat(val)
vDate(val, nFormat)
////////////
//
// Validation Library
//
// (c) Jacek Murawski, Kozienice 1999
//
// e-mail: jacek.murawski@elko.com.pl
// http:// www.chinasoft.com.pl
//
////////////
// funkcje sprawdzania poprawnosci danych nie nalezaca do
// zadnej klasy
// Dostepne funkcje:
//
// vLength(val, nLen)
// vRequired(val)
// vIsInteger(val)
// vIsFloat(val)
// vDate(val, nFormat)
// 0 - dd/mm/rrrr
// 1 - mm/dd/rrrr
// 2 - rrrr/mm/dd
//
// Do zrobienia:
//
// vEmail(val)
// vRangeInt(val, nFrom, nTo)
// vRangeFloat(val, nFrom, nTo)
//
// funkcja sprawdzajaca, czy wartosc pola nie jest dluzsza niz ...
function vLength(val, nLen)
{
if (val.length > nLen)
return false;
else
return true;
}
// funkcja sprawdza, czy wartosc pola > 0
function vRequired(val)
{
if (val.length < 1)
return false;
else
return true;
}
// sprawdzenie czy w polu liczba Integer
function vInteger(val)
{
var i = 0;
var is_ok = true;
var number = "+-0123456789";
if (number.indexOf(val.charAt(0)) == -1)
is_ok = false;
number = number.substring(2, 20);
for (i = 1; i < val.length; i++)
{
if (number.indexOf(val.charAt(i)) == -1)
is_ok = false;
}
return is_ok
}
// sprawdzenie, czy w polu liczba float
function vFloat(val)
{
var i = 0;
var is_ok = true;
var number = "+-0123456789.";
if (number.indexOf(val.charAt(0)) == -1)
is_ok = false;
number = number.substring(2, 20);
for (i = 1; i < val.length; i++)
{
if (number.indexOf(val.charAt(i)) == -1)
is_ok = false;
}
return is_ok;
}
// sprawdzenie czy w polu prawidlowa data
// 0 - dd/mm/rrrr
// 1 - mm/dd/rrrr
// 2 - rrrr/mm/dd
// 1234567890
function vDate(val, format)
{
var i = 0;
var is_ok = true;
var number = "0123456789";
var s;
if (format == null)
format = 0;
// sprawdzenie dlugosci
if (val.length != 10)
return false;
if (format == 0)
{
s_month = val.substring(0,2);
s_day = val.substring(3,5);
s_year = val.substring(6,10);
// separatory
if ((val.charAt(2) != "/") || (val.charAt(5) != "/"))
return false;
}
// dni
for (i = 0; i < 2; i++)
{
if (number.indexOf(s_day.charAt(i)) == -1)
is_ok = false;
}
if (is_ok)
nDay = parseInt(s_day);
else
return false;
// miesiace
for (i = 0; i < 2; i++)
{
if (number.indexOf(s_month.charAt(i)) == -1)
is_ok = false;
}
if (is_ok)
nMonth = parseInt(s_month);
else
return false;
// lata
for (i = 0; i < 4; i++)
{
if (number.indexOf(s_year.charAt(i)) == -1)
is_ok = false;
}
if (is_ok)
nYear = parseInt(s_year);
else
return false;
return is_ok
}
//////////////////////////////////////////////////////
//
// definicja klasy TValidation
//
//////////////////////////////////////////////////////
////////////////// Klasa TField /////////////////////
function TField()
{
this.Name = "";
this.Message = "";
this.isValid = false;
this.Code = "";
this.SetName = TField_SetName;
this.SetMessage = TField_SetMessage;
this.SetIsValid = TField_SetIsValid;
this.SetCode = TField_SetCode;
this.GetName = TField_GetName;
this.GetMessage = TField_GetMessage;
this.GetIsValid = TField_GetIsValid;
this.GetCode = TField_GetCode;
// sprawdzanie poprawnosci pola
this.Check = TField_Check;
}
function TField_SetName(cName)
{
this.Name = cName;
}
function TField_SetMessage(cMessage)
{
this.Message = cMessage;
}
function TField_SetIsValid(bIsValid)
{
this.isValid = bIsValid;
}
function TField_SetCode(cCode)
{
return this.Code = cCode;
}
function TField_GetName()
{
return this.Name;
}
function TField_GetMessage()
{
return this.Message;
}
function TField_GetIsValid()
{
return this.isValid;
}
function TField_GetCode()
{
return this.Code;
}
// sprawdzanie poprawnosci pola
function TField_Check(cForm)
{
var v = eval("document." + cForm + "." + this.Name + ".value");
this.isValid = eval(this.Code);
// ewentualna zmiana zawartosci pola
// if (!this.isValid)
// {
// cNew = v + "???";
// eval("document." + cForm + "." + this.Name + ".value = " + "\""+cNew+"\"");
// }
}
/////////////////// Klasa TListField ////////////////////
function TListField()
{
this.List = new Array(1);
this.Counter = 0;
this.Add = TListField_Add;
this.GetMsg = TListField_GetMsg;
this.IsValid = TListField_IsValid;
this.Check = TListField_Check;
this.Clear = TListField_Clear;
this.BuildMsg = TListField_BuildMsg;
}
// dodanie wiersz do listy pol
function TListField_Add(cName, cMsg, cCode)
{
var fld = new TField;
fld.SetName(cName);
fld.SetMessage(cMsg);
fld.SetIsValid(false);
fld.SetCode(cCode);
this.List[this.Counter] = fld;
this.Counter++;
}
// czyszczenie listy
function TListField_Clear()
{
this.Counter = 0;
}
// pobranie komunikatu
function TListField_GetMsg(nRow)
{
var fld = new TField;
fld = this.List[nRow];
return fld.GetMessage();
}
function TListField_IsValid(nRow)
{
var fld = new TField;
fld = this.List[nRow];
return fld.GetIsValid();
}
// sprawdzenie listy pol
function TListField_Check(cForm)
{
var fld = new TField;
var isOk = true;
for (i = 0; i < this.Counter; i++)
{
fld = this.List[i];
fld.Check(cForm);
this.List[i] = fld;
if (!fld.GetIsValid())
isOk = false;
}
return isOk;
}
// budowanie komunikatu bledu
function TListField_BuildMsg()
{
var fld = new TField;
var cMsg = ""
for (i = 0; i < this.Counter; i++)
{
fld = this.List[i];
if (!fld.GetIsValid())
{
cMsg = cMsg + fld.GetMessage() + "\n";
// window.alert(cMsg);
}
}
return cMsg;
}
/////////////////////// klasa TValidation ////////////////////
function TValidation(cForm)
{
this.FormName = cForm;
this.List = new TListField();
this.AddItem = TValidation_AddItem;
this.Check = TValidation_Check;
this.MessageWin = TValidation_MessageWin;
}
// dodanie elementu
function TValidation_AddItem(cName, cMsg, cCode)
{
this.List.Add(cName, cMsg, cCode);
}
// sprawdzenie formularza
function TValidation_Check()
{
if (this.List.Check(this.FormName))
return true;
else
{
this.MessageWin();
return false;
}
}
// jezeli weryfikacja niepoprawna to
// wyswietlenie okna z komunikatem bledu
function TValidation_MessageWin()
{
cMsg = "Dane niepoprawne:\n";
cMsg2 = this.List.BuildMsg();
window.alert(cMsg+cMsg2);
}