I need some help regarding javascript textbox validation. And there are 6 text boxes per row, totally 5 rows that are displaying using for loop.
I have done with the validation for all the 5 rows. Here is the code for that..!!
The total Javascript I am using...
Code:
<script language="javascript">
function Validate()
{
var rgx = /(\d{4})-(\d{2})-(\d{2})/;
var rgxa = /^-?\d+(\.\d+)?$/;
// var totrows = document.getElementById("totalrows[]").value
dml=document.forms['travel'];
// get the number of elements from the document
len = dml.elements.length;
for( i=0;i<len;i++)
{
//check the textbox with the elements name
if (dml.elements[i].name=='date[]')
{
// if exists do the validation and set the focus to the textbox
if (dml.elements[i].value=="")
{
alert("Please Enter the Date..!!");
dml.elements[i].focus();
return false;
}
if(!dml.elements[i].value.match(rgx))
{
alert("Invalid Date Format..!!");
dml.elements[i].value="";
dml.elements[i].focus();
return false;
}
}
if (dml.elements[i].name=='from[]')
{
// if exists do the validation and set the focus to the textbox
if (dml.elements[i].value=="")
{
alert("Please Enter the Starting Place ..!!");
dml.elements[i].focus();
return false;
}
}
if (dml.elements[i].name=='destination[]')
{
// if exists do the validation and set the focus to the textbox
if (dml.elements[i].value=="")
{
alert("Please Enter the Destination Place ..!!");
dml.elements[i].focus();
return false;
}
}
if (dml.elements[i].name=='purpose[]')
{
// if exists do the validation and set the focus to the textbox
if (dml.elements[i].value=="")
{
alert("Please Enter the Purpose ..!!");
dml.elements[i].focus();
return false;
}
}
if (dml.elements[i].name=='mode[]')
{
// if exists do the validation and set the focus to the textbox
if (dml.elements[i].value=="")
{
alert("Please Enter the mode of travelling ..!!");
dml.elements[i].focus();
return false;
}
}
if (dml.elements[i].name=='amount[]')
{
// if exists do the validation and set the focus to the textbox
if (dml.elements[i].value=="")
{
alert("Please Enter the Amount ..!!");
dml.elements[i].focus();
return false;
}
if(!rgxa.test(dml.elements[i].value))
{
alert("Not a Valid Amount, Enter in Decimals..!!");
dml.elements[i].value="";
dml.elements[i].focus();
return false;
}
}
}
return true;
}
</script>
This works prefect. But I dont need to validate all the 5 rows at a time. First the user enters the data in the first row(It is compulsory). And if he dont hav data to be inserted in the second row then it should submit the page and insert the first row data in the database. This is my requirement.
Suppose if he wants to insert in the second row then it should validate the second row. (I need this, dont know how to make it with the above code)
Some one please go through the code and gimme some solution.
I didnt' Understand this..Also I tried the code u posted in the previous thread..But it is not focusing to any fields...
Once the alert box appears for date it should focus to date text box, next if alert comes for starting place it should focus to starting text box..etc etc..for all the textboxes...
function Check(){
var flds1=document.getElementsByName('date[]');
var flds2=document.getElementsByName('from[]');
var flds3=document.getElementsByName('destination[]');
var flds4=document.getElementsByName('purpose[]');
var flds5=document.getElementsByName('amount[]');
var funs=['CkDate','CkEmpty','CkEmpty','CkEmpty','CkFloat'];
var messary=["The Date, Please Enter a valid Date","From, Please Enter the Starting Place","Destintion, Please Enter the Destintion","Purpose, Please enter the purpose","Amount, Please enter in Decimals"];
var mess=[];
for (var zxc0=0;zxc0<flds1.length;zxc0++){
var flds=[flds1[zxc0],flds2[zxc0],flds3[zxc0],flds4[zxc0],flds5[zxc0]];
if (zxc0==0||CkAny(flds)){
for (var zxc0a=0;zxc0a<flds.length;zxc0a++){
flds[zxc0a].style.borderColor='#ffffff';
if (!window[funs[zxc0a]](flds[zxc0a])){
flds[zxc0a].style.borderColor='#ff0000';
mess.push('Row: '+(zxc0+1)+' '+messary[zxc0a]);
mess.lst=flds[0];
}
}
}
if (zxc0==0&&mess.length>0){
mess.push('Row 1 is Mandatory!!');
for (var zxc0b=0;zxc0b<flds1.length;zxc0b++){
var flds=[flds1[zxc0b],flds2[zxc0b],flds3[zxc0b],flds4[zxc0b],flds5[zxc0b]];
for (var zxc0c=0;zxc0c<flds.length;zxc0c++){
flds[zxc0c].value='';
}
}
break;
}
}
if (mess.length>0){
alert(mess.join('\n'));
if (mess.lst) mess.lst.focus();
return false;
}
return true;
}
function CkDate(el){
var rgx = /(\d{4})-(\d{2})-(\d{2})/;
if(!el.value.match(rgx)){
el.value="";
return false;
}
return true;
}
function CkFloat(el){
var rgxa = /^-?\d+(\.\d+)?$/;
if(!rgxa.test(el.value)){
el.value="";
return false;
}
return true;
}
function CkEmpty(el){
return (el.value.replace(/\s/g,'')=='')?false:true;
}
function CkAny(ary){
for (var zxc0=0;zxc0<ary.length;zxc0++){
if (ary[zxc0].value.replace(/\s/g,'')!='') return true;
}
return false;
}
unable to submit once validatoin is completed for first row.
I dont need to validate all the 2 rows at a time. First the user enters the data in the first row(It is compulsory). And if he dont hav data to be inserted in the second row then it should submit the page for the action="sample.asp" and insert the first row data in the database. This is my requirement.
Suppose if he wants to insert in the second row then it should validate the second row. (I need this, dont know how to make it with the above code)
function Check(){
var flds1=document.getElementsByName('Friendname');
var flds2=document.getElementsByName('Friendemail');
var funs=['CkEmpty','CkEmail'];
var messary=["From, Please Enter the Sendername","Senderemail, Please Enter the Senderemail"];
var mess=[];
for (var zxc0=0;zxc0<flds1.length;zxc0++){
var flds=[flds1[zxc0],flds2[zxc0]];
if (zxc0==0||CkAny(flds)){
for (var zxc0a=0;zxc0a<flds.length;zxc0a++){
flds[zxc0a].style.border='1px solid #ABADB3';
if (!window[funs[zxc0a]](flds[zxc0a])){
flds[zxc0a].style.border='1px solid #ff0000';
mess.push('Row: '+(zxc0+1)+' '+messary[zxc0a]);
mess.lst=flds[0];
}
}
}
if (zxc0==0&&mess.length>0){
mess.push('Row 1 is Mandatory!!');
for (var zxc0b=0;zxc0b<flds1.length;zxc0b++){
var flds=[flds1[zxc0b],flds2[zxc0b]];
for (var zxc0c=0;zxc0c<flds.length;zxc0c++){
flds[zxc0c].value='';
}
}
break;
}
}
if (mess.length>0){
alert(mess.join('\n'));
if (mess.lst) mess.lst.focus();
return false;
}
return true;
}
function CkEmail(el){
var rgx = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/ ;
if(!el.value.match(rgx)){
el.value="";
return false;
}
return true;
}
function CkFloat(el){
var rgxa = /^-?\d+(\.\d+)?$/;
if(!rgxa.test(el.value)){
el.value="";
return false;
}
return true;
}
function CkEmpty(el){
return (el.value.replace(/\s/g,'')=='')?false:true;
}
function CkAny(ary){
for (var zxc0=0;zxc0<ary.length;zxc0++){
if (ary[zxc0].value.replace(/\s/g,'')!='') return true;
}
return false;
}
Bookmarks