Jump to content

Form Validation php


gorex

Recommended Posts

Hi. Does anyone know anyway to put in additional php coding to validate list/menu in forms? I am currently using Dreamweaver, so I am using their Validation check in their Tags/Behaviour sections. I read in alot of places that this default function does not allow a validation check for list/menu. But I am wondering if anyone know of anyway to put in some additional coding so that it will allow it to validate the list/menu as well? Thanks.

Link to comment
https://forums.phpfreaks.com/topic/61851-form-validation-php/
Share on other sites

Sorry. Not used to asking questions like this in forum :-[...  ok, here goes... I basically used the validation function in Dreamweaver to validate my form. I am not very good at php, but from what I can see, it added in this piece of coding:

 

function MM_validateForm() { //v4.0

  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;

  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args);

    if (val) { nm=val.name; if ((val=val.value)!="") {

      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');

        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';

      } else if (test!='R') { num = parseFloat(val);

        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';

        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');

          min=test.substring(8,p); max=test.substring(p+1);

          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';

    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }

  } if (errors) alert('The following error(s) occurred:\n'+errors);

  document.MM_returnValue = (errors == '');

}

 

So you can see that it will validate things like empty fields, email checking for email field, and numbers in a number field. However, it will not check a list/menu. For example I got a list menu which list 1 to 10, and the default is just - - - - . I was thinking of maybe adding a piece of coding so that it will check that if the value of the list is  - - - -, then it will pop up an error in the same way that it pops up error for the validation, and say that a selection is needed from the list/menu? Not sure if that can be done, though...

 

Its also got this piece of coding to identify each field in the form:

 

<body onload="MM_validateForm('Forename','','R','Surname','','R','Address','','R','County','','R','Postcode','','R','Email','','RisEmail','Telephone','','RisNum');return document.MM_returnValue">

Link to comment
https://forums.phpfreaks.com/topic/61851-form-validation-php/#findComment-308049
Share on other sites

Opps.. sorry for adding another post. I got another field in the form which is for them to make some selections. For simplicity, lets just say that the options are 1-10 and the default is ----. The name of the field is count. Is there anything I can add in the code so that I can ask it to make sure that the menu is selected other than ----. Thanks

Link to comment
https://forums.phpfreaks.com/topic/61851-form-validation-php/#findComment-308051
Share on other sites

Sorry ViN86. But is there a way to set it as part of this code? This is so that it will popup the error msg together with the rest of the form validation.

 

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}

 

 

Link to comment
https://forums.phpfreaks.com/topic/61851-form-validation-php/#findComment-308081
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.