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
Share on other sites

No one?  :(

 

well, youre being kind of vague.  post some code, show us what you want to validate. :)

 

ie, do you want to validate the form (using regular expressions and what not), do you want to make sure a value is entered before the process continues, etc.

 

 

Link to comment
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
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
Share on other sites

First, post to another page....

 

then...

 

$value = $_POST['name_of_list_to_validate']

 

<?php
if ($value != "----"){
    // code for when a value has been selected
} else {
    // code for when a value has not been selected, usually error reporting code
}
?>

Link to comment
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.