www.webdeveloper.com
Results 1 to 9 of 9

Thread: Javascript Multiple Textbox validation..!!

  1. #1
    Join Date
    Apr 2008
    Location
    Mumbai, India
    Posts
    259

    Question Javascript Multiple Textbox validation..!!

    Hi everyone..

    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.

    Any help is greatly Appreciated...

    Many thanks in Advance...!!

  2. #2
    Join Date
    Jun 2004
    Location
    Portsmouth UK
    Posts
    2,674
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script language="JavaScript" type="text/javascript">
    /*<![CDATA[*/
    
    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++){
        if (!window[funs[zxc0a]](flds[zxc0a])){
         mess.push('Row: '+(zxc0+1)+' '+messary[zxc0a]);
        }
       }
      }
      if (zxc0==0&&mess.length>0){
       mess.push('Row 1 is Mandatory!!');
      }
     }
     if (mess.length>0){
      alert(mess.join('\n'));
      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;
    }
    
    
    /*]]>*/
    </script>
    </head>
    
    <body>
    <form name="">
    date <input name="date[]" size="10" />from<input name="from[]" size="10" />destination<input  name="destination[]" size="10" />purpose<input name="purpose[]"  size="10"/>amount<input  name="amount[]"  size="10"/><br />
    date <input name="date[]" size="10" />from<input name="from[]" size="10" />destination<input  name="destination[]" size="10" />purpose<input name="purpose[]"  size="10"/>amount<input  name="amount[]"  size="10"/><br />
    date <input name="date[]" size="10" />from<input name="from[]" size="10" />destination<input  name="destination[]" size="10" />purpose<input name="purpose[]"  size="10"/>amount<input  name="amount[]"  size="10"/><br />
    date <input name="date[]" size="10" />from<input name="from[]" size="10" />destination<input  name="destination[]" size="10" />purpose<input name="purpose[]"  size="10"/>amount<input  name="amount[]"  size="10"/><br />
    date <input name="date[]" size="10" />from<input name="from[]" size="10" />destination<input  name="destination[]" size="10" />purpose<input name="purpose[]"  size="10"/>amount<input  name="amount[]"  size="10"/><br />
    <input type="button" name="" value="Test" onclick="Check();"/>
    </form>
    </body>
    
    </html>
    Vic

    God loves you and will never love you less.

    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. #3
    Join Date
    Apr 2008
    Location
    Mumbai, India
    Posts
    259
    Dear vwphillips..!!

    Your code is Simpy Superb..Its perfect...

    You are an Expert...

    Many Many thanks..!!

    One more simple probelm...I want to focus on the specified field after alert..for example..after date validation it should focus to date text box...

    Once again many thanks...

  4. #4
    Join Date
    Jun 2004
    Location
    Portsmouth UK
    Posts
    2,674
    should focus to date text box...
    only the last failing field will be focused but
    Code:
    <head>
      <title></title>
    <script language="JavaScript" type="text/javascript">
    /*<![CDATA[*/
    
    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.backgroundColor='#ffffff';
        if (!window[funs[zxc0a]](flds[zxc0a])){
         flds[zxc0a].style.backgroundColor='#ff0000';
         mess.push('Row: '+(zxc0+1)+' '+messary[zxc0a]);
         mess.lst=flds[zxc0a];
        }
       }
      }
      if (zxc0==0&&mess.length>0){
       mess.push('Row 1 is Mandatory!!');
      }
     }
     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;
    }
    
    
    /*]]>*/
    </script>
    </head>
    
    <body>
    <form name="">
    date <input name="date[]" size="10" />from<input name="from[]" size="10" />destination<input  name="destination[]" size="10" />purpose<input name="purpose[]"  size="10"/>amount<input  name="amount[]"  size="10"/><br />
    date <input name="date[]" size="10" />from<input name="from[]" size="10" />destination<input  name="destination[]" size="10" />purpose<input name="purpose[]"  size="10"/>amount<input  name="amount[]"  size="10"/><br />
    date <input name="date[]" size="10" />from<input name="from[]" size="10" />destination<input  name="destination[]" size="10" />purpose<input name="purpose[]"  size="10"/>amount<input  name="amount[]"  size="10"/><br />
    date <input name="date[]" size="10" />from<input name="from[]" size="10" />destination<input  name="destination[]" size="10" />purpose<input name="purpose[]"  size="10"/>amount<input  name="amount[]"  size="10"/><br />
    date <input name="date[]" size="10" />from<input name="from[]" size="10" />destination<input  name="destination[]" size="10" />purpose<input name="purpose[]"  size="10"/>amount<input  name="amount[]"  size="10"/><br />
    <input type="button" name="" value="Test" onclick="Check();"/>
    </form>
    </body>
    
    </html>
    Vic

    God loves you and will never love you less.

    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  5. #5
    Join Date
    Apr 2008
    Location
    Mumbai, India
    Posts
    259
    only the last failing field will be focused but
    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...

    Could you please tel me how can I do that??

  6. #6
    Join Date
    Jun 2004
    Location
    Portsmouth UK
    Posts
    2,674
    by definition ony one element can be focused.

    the posted code focusesthe last failing field and highlights all failing fields in red

    this focuses the last date field in the last failing row and clears all other rows if row 0 fails.

    Code:
    <head>
      <title></title>
    <script language="JavaScript" type="text/javascript">
    /*<![CDATA[*/
    
    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.backgroundColor='#ffffff';
        if (!window[funs[zxc0a]](flds[zxc0a])){
         flds[zxc0a].style.backgroundColor='#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;
    }
    
    
    /*]]>*/
    </script>
    </head>
    
    <body>
    <form name="">
    date <input name="date[]" size="10" />from<input name="from[]" size="10" />destination<input  name="destination[]" size="10" />purpose<input name="purpose[]"  size="10"/>amount<input  name="amount[]"  size="10"/><br />
    date <input name="date[]" size="10" />from<input name="from[]" size="10" />destination<input  name="destination[]" size="10" />purpose<input name="purpose[]"  size="10"/>amount<input  name="amount[]"  size="10"/><br />
    date <input name="date[]" size="10" />from<input name="from[]" size="10" />destination<input  name="destination[]" size="10" />purpose<input name="purpose[]"  size="10"/>amount<input  name="amount[]"  size="10"/><br />
    date <input name="date[]" size="10" />from<input name="from[]" size="10" />destination<input  name="destination[]" size="10" />purpose<input name="purpose[]"  size="10"/>amount<input  name="amount[]"  size="10"/><br />
    date <input name="date[]" size="10" />from<input name="from[]" size="10" />destination<input  name="destination[]" size="10" />purpose<input name="purpose[]"  size="10"/>amount<input  name="amount[]"  size="10"/><br />
    <input type="button" name="" value="Test" onclick="Check();"/>
    </form>
    </body>
    
    </html>
    Vic

    God loves you and will never love you less.

    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  7. #7
    Join Date
    Sep 2012
    Posts
    3

    unable to submit once validatoin is completed. Can any one help here..

    <head>
    <title></title>
    <script language="JavaScript" type="text/javascript">
    /*<![CDATA[*/

    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;
    }


    /*]]>*/
    </script>
    <style>
    .formError {border:1px solid red!important;}
    </style>
    </head>

    <body>
    <form name="">
    date <input name="date[]" size="10" />from<input name="from[]" size="10" />destination<input name="destination[]" size="10" />purpose<input name="purpose[]" size="10"/>amount<input name="amount[]" size="10"/><br />
    date <input name="date[]" size="10" />from<input name="from[]" size="10" />destination<input name="destination[]" size="10" />purpose<input name="purpose[]" size="10"/>amount<input name="amount[]" size="10"/><br />
    date <input name="date[]" size="10" />from<input name="from[]" size="10" />destination<input name="destination[]" size="10" />purpose<input name="purpose[]" size="10"/>amount<input name="amount[]" size="10"/><br />
    date <input name="date[]" size="10" />from<input name="from[]" size="10" />destination<input name="destination[]" size="10" />purpose<input name="purpose[]" size="10"/>amount<input name="amount[]" size="10"/><br />
    date <input name="date[]" size="10" />from<input name="from[]" size="10" />destination<input name="destination[]" size="10" />purpose<input name="purpose[]" size="10"/>amount<input name="amount[]" size="10"/><br />
    <input type="button" name="" value="Test" onclick="Check();"/>
    </form>
    </body>

    </html>

  8. #8
    Join Date
    Sep 2012
    Posts
    3

    unable to submit once validatoin is completed. Can any one help here..

    Quote Originally Posted by vwphillips View Post
    by definition ony one element can be focused.

    the posted code focusesthe last failing field and highlights all failing fields in red

    this focuses the last date field in the last failing row and clears all other rows if row 0 fails.

    Code:
    <head>
      <title></title>
    <script language="JavaScript" type="text/javascript">
    /*<![CDATA[*/
    
    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.backgroundColor='#ffffff';
        if (!window[funs[zxc0a]](flds[zxc0a])){
         flds[zxc0a].style.backgroundColor='#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;
    }
    
    
    /*]]>*/
    </script>
    </head>
    
    <body>
    <form name="">
    date <input name="date[]" size="10" />from<input name="from[]" size="10" />destination<input  name="destination[]" size="10" />purpose<input name="purpose[]"  size="10"/>amount<input  name="amount[]"  size="10"/><br />
    date <input name="date[]" size="10" />from<input name="from[]" size="10" />destination<input  name="destination[]" size="10" />purpose<input name="purpose[]"  size="10"/>amount<input  name="amount[]"  size="10"/><br />
    date <input name="date[]" size="10" />from<input name="from[]" size="10" />destination<input  name="destination[]" size="10" />purpose<input name="purpose[]"  size="10"/>amount<input  name="amount[]"  size="10"/><br />
    date <input name="date[]" size="10" />from<input name="from[]" size="10" />destination<input  name="destination[]" size="10" />purpose<input name="purpose[]"  size="10"/>amount<input  name="amount[]"  size="10"/><br />
    date <input name="date[]" size="10" />from<input name="from[]" size="10" />destination<input  name="destination[]" size="10" />purpose<input name="purpose[]"  size="10"/>amount<input  name="amount[]"  size="10"/><br />
    <input type="button" name="" value="Test" onclick="Check();"/>
    </form>
    </body>
    
    </html>

  9. #9
    Join Date
    Sep 2012
    Posts
    3

    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)

    Below is the code i am using:

    <head>
    <title></title>
    <script language="JavaScript" type="text/javascript">
    /*<![CDATA[*/

    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;
    }


    /*]]>*/
    </script>
    </head>

    <body>
    <form name="form" action="sample.asp">


    Friendname <input name="Friendname" size="10" />Friendemail<input name="Friendemail" size="10" /><br />
    Friendname <input name="Friendname" size="10" />Friendemail<input name="Friendemail" size="10" /><br />
    Friendname <input name="Friendname" size="10" />Friendemail<input name="Friendemail" size="10" /><br />

    <input type="button" name="" value="Submit" onclick="Check();"/>
    </form>
    </body>

    </html>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles