Results 1 to 6 of 6
  1. #1
    2 Star Lounger
    Join Date
    Apr 2003
    Posts
    139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Perl - Validating Fields

    Hi everyone --

    I am a total newbie when it comes to Perl (just started in the last week) -- I have the ubiquitous O'Reilly manual on Perl, and have done a few online tutorials, but that's about it.

    I have a form that I have put on the web. When a user hits the "submit query" button on this form, it calls a .cgi script that will compile the information and send it to a specified address via email. I have a few required fields on here, the most important of which is the disclaimer -- a check box that the user needs to click. How do I get the form to NOT send data if that check box hasn't been checked? I would also like there to be a popup box that says "Please verify that you have done this...blah blah."

    Thanks for any help!
    Kindra

  2. #2
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    1,862
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Perl - Validating Fields

    Hi Kindra,

    This is a task for Javascript. There are several ways to produce the desired results. The concept is that you will wire the form's Submit button to trigger a Javascript function that checks for the desired conditions (like a checked checkbox). If the conditions are true, the form is allowed to submit. Otherwise a message box (javascript's alert() function) will display.

    You'll have to forgive me because I'm a bit too rusty with Javascript to write the code from the top of my head. I've always found excellent instruction and examples at W3Schools. You may also consider searching Google for code examples using "Javascript validation" as search terms.

    Hope this helps...

  3. #3
    2 Star Lounger
    Join Date
    Apr 2003
    Posts
    139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Perl - Validating Fields

    So would I put the alert code into my HTML document or my .cgi document?

    Thanks for the site reference -- it's very cool!

  4. #4
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    1,862
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Perl - Validating Fields

    This will go into your HTML document. It will run as a client-side feature.

    You can also use server-side validation, which checks the form results AFTER the form has been submitted. The only catch is that you will usually submit the form back to itself for checking. After the results are validated you can post the final results to another form which will do the processing. But I digress...

    Here's a quick and (very) dirty sample for you (note the spaces left in the HTML tags for Lounge display purposes):<pre>< html >
    < head >
    < script language="javascript" >
    function validateForm() {
    if (document.myForm.txtField1.length > 0 && document.myForm.chkCheckbox.checked == true)
    {
    myForm.submit;
    }
    else alert('Please fill in Field1 and check Checkbox!');
    }
    < /script >
    < body >

    < form id="myForm" action="mypage.cgi" method="post" >
    Field1:< input type="text" id="txtField1" >
    Checkbox:< input type="checkbox" id="chkCheckbox" >
    < input type="button" onclick="java script:validateForm();" value="Click Me" >
    < /form >
    < /body >
    < /html ></pre>

    (As noted before, I'm VERY rusty on writing this stuff from scratch. I'm spoiled by ASP.NET and the Visual Studio 2003 IDE... Please forgive any typos or blatant syntatical errors...)

  5. #5
    5 Star Lounger jujuraf's Avatar
    Join Date
    Jun 2001
    Location
    San Jose, California, USA
    Posts
    1,061
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Perl - Validating Fields

    In addition to what MarkJ said (using JS to initially validate the form) I'd also put this same validation in your Perl script. I do this myself just in case you have a user who has JS disabled. It is twice the work but ensures it works in all situations.

    Congrats on trying to learn Perl and form validation. It's kind of fun I think and it really teaches you a lot about how a web page is rendered, how a web server works, etc. All good stuff to know. <img src=/S/clapping.gif border=0 alt=clapping width=19 height=23>

    For the Perl side, I hope you're using the module CGI.pm as this makes form validation very easy. I would highly recommend Lincoln Stein's book on CGI.pm as he wrote the module, http://stein.cshl.org/WWW/software/CGI/ and http://stein.cshl.org/~lstein/. This module also makes it much easier to dynamically create a web page. So if you want to send a custom acknowledgement page (thank you page) to a viewer with their name, or read data from a database to use to populate a list box (instead of hard-coding these items).

    In essence, what you do is have this module fetch all the field names (and their values) in your form. You then have code that checks if these values are what you require. For instance, if a field is named txtPhone and the value is 'ABCD123' then your code can check if this is a valid phone number (JS can also do this).

    Have fun,
    Deb

  6. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Perl - Validating Fields

    Following up on Deb's comment, all validation should be re-done on the server side because there are tools that can insert different input into the form submission after the Submit button is clicked. (These tools are used by security consultants and those the security consultants are trying to catch, if you know what I mean.)

Posting Permissions

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