Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Senior Coder
    Join Date
    Jul 2003
    Location
    My pimped-out igloo in Canadia
    Posts
    1,966
    Thanks
    36
    Thanked 0 Times in 0 Posts

    form validation modification

    Hey. I'm using a really nifty form validator, but i want the cursor to go back into the first form field that isn't valid

    i wanted to incorporate something like -- onError (if e-mail was the first incorrect field:
    Code:
     document.productchoice.email.focus();
    into the script below, but dont know how.

    this goes in the head:
    Code:
    function MM_findObj(n, d) { //v4.01
      var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
        d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
      if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
      for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
      if(!x && d.getElementById) x=d.getElementById(n); return x;
    }
    
    
    function YY_checkform() { 
    
      var a=YY_checkform.arguments,oo=true,v='',s='',err=false,r,o,at,o1,t,i,j,ma,rx,cd,cm,cy,dte,at;
      for (i=1; i<a.length;i=i+4){
        if (a[i+1].charAt(0)=='#'){r=true; a[i+1]=a[i+1].substring(1);}else{r=false}
        o=MM_findObj(a[i].replace(/\[\d+\]/ig,""));
        o1=MM_findObj(a[i+1].replace(/\[\d+\]/ig,""));
        v=o.value;t=a[i+2];
        if (o.type=='text'||o.type=='password'||o.type=='hidden'){
          if (r&&v.length==0){err=true}
          if (v.length>0)
          if (t==1){ //fromto
            ma=a[i+1].split('_');if(isNaN(v)||v<ma[0]/1||v > ma[1]/1){err=true}
          } else if (t==2){
            rx=new RegExp("^[\\w\.=-]+@[\\w\\.-]+\\.[a-zA-Z]{2,4}$");if(!rx.test(v))err=true;
          } else if (t==3){ // date
            ma=a[i+1].split("#");at=v.match(ma[0]);
            if(at){
              cd=(at[ma[1]])?at[ma[1]]:1;cm=at[ma[2]]-1;cy=at[ma[3]];
              dte=new Date(cy,cm,cd);
              if(dte.getFullYear()!=cy||dte.getDate()!=cd||dte.getMonth()!=cm){err=true};
            }else{err=true}
          } else if (t==4){ // time
            ma=a[i+1].split("#");at=v.match(ma[0]);if(!at){err=true}
          } else if (t==5){ // check this 2
                if(o1.length)o1=o1[a[i+1].replace(/(.*\[)|(\].*)/ig,"")];
                if(!o1.checked){err=true}
          } else if (t==6){ // the same
                if(v!=MM_findObj(a[i+1]).value){err=true}
          }
        } else
        if (!o.type&&o.length>0&&o[0].type=='radio'){
              at = a[i].match(/(.*)\[(\d+)\].*/i);
              o2=(o.length>1)?o[at[2]]:o;
          if (t==1&&o2&&o2.checked&&o1&&o1.value.length/1==0){err=true}
          if (t==2){
            oo=false;
            for(j=0;j<o.length;j++){oo=oo||o[j].checked}
            if(!oo){s+='* '+a[i+3]+'\n'}
          }
        } else if (o.type=='checkbox'){
          if((t==1&&o.checked==false)||(t==2&&o.checked&&o1&&o1.value.length/1==0)){err=true}
        } else if (o.type=='select-one'||o.type=='select-multiple'){
          if(t==1&&o.selectedIndex/1==0){err=true}
        }else if (o.type=='textarea'){
          if(v.length<a[i+1]){err=true}
        }
        if (err){s+='* '+a[i+3]+'\n'; err=false}
      }
      if (s!=''){alert('Before the quote request may be submitted, we request that you:\t\t\t\t\t\n\n'+s)}
      document.MM_returnValue = (s=='');
    }
    //-->
    </SCRIPT>
    and this is the form header
    Code:
    <form action=../../cgi-bin/TFmail.pl method=post name="productchoice" onSubmit="YY_checkform('productchoice','Producer','#q','1','Please select a company','Product','#q','1','Please select a product','Quantity','#1_1000000','1','Please enter the quantity of units you wish to be quoted on \(or on estimate thereof\)','Company','#q','0','Please enter your company name','FirstName','#q','0','Please enter your first name','LastName','#q','0','Please enter your last name','Address','#q','0','Please enter your company\'s address','City','#q','0','Please enter the city in which your company resides','Province','#q','0','Please enter the province in which your company resides','PostalCode','#q','0','Please enter your company\'s postal / zip code','email','#S','2','Please enter a valid e-mail address','Telephone','#q','0','Please enter a telephone number at which you may be contacted','Application_Information','1','1','Please enter information regarding the intended use of the chosen product');return document.MM_returnValue">
    how do i do this? or can i?
    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, you're a mile away and you have their shoes :)

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    hm... rather intricate those MM code lines...

    Try insert the red colored

    if (s!=''){alert('Before the quote request may be submitted, we request that you:\t\t\t\t\t\n\n'+s);o.focus()}
    document.MM_returnValue = (s=='');
    }
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Kor
    hm... rather intricate those MM code lines...
    Hmmm;

    Yeppers, there's alot to be said of trying to read mickey mouse code with no formating and non-intuitive variable names.

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    mickey mouse code
    This is a great one! I'll remember it to use it some other time
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The developers of Dreamweaver had a sense of humour.

    MM == Mickey Mouse

  • #6
    Senior Coder
    Join Date
    Jul 2003
    Location
    My pimped-out igloo in Canadia
    Posts
    1,966
    Thanks
    36
    Thanked 0 Times in 0 Posts
    i agree the code / formatting thereof is garbage. it was spit out by a dreamweaver extension for form validation.

    i take no credit / blame for it's poor construction

    i wonder what YY stands for then
    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, you're a mile away and you have their shoes :)

  • #7
    Senior Coder
    Join Date
    Jul 2003
    Location
    My pimped-out igloo in Canadia
    Posts
    1,966
    Thanks
    36
    Thanked 0 Times in 0 Posts
    okay... we're on the right track. that set the focus to the last field on the form after it validates... now all thats needed is to get it to set it to the first caught error.

    i suppose we could create a variable, and have it set it to 1 the first time it catches an invalid entry in a field, then have the focus set to the field with variable = 1 (or true, or whatever)

    would this be too complicated to do?

    i can paste the entire form if you guys want..
    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, you're a mile away and you have their shoes :)

  • #8
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    well I confess that I was not able to follow the entire mickey mouse code in amount, but I supposed that the general ideea was to give a return false if the first text field is found not filled in the right mode. Thus, I presumed, the rest of the code will have not to be passed, so it should be the first text field with problems to be focused each time...

    Wouldn't be easier to build another "lighter" script for?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #9
    Senior Coder
    Join Date
    Jul 2003
    Location
    My pimped-out igloo in Canadia
    Posts
    1,966
    Thanks
    36
    Thanked 0 Times in 0 Posts
    i like your approach

    now.. hmm, how to code it...
    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, you're a mile away and you have their shoes :)


  •  

    Posting Permissions

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