Rocko19 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am working on a application that has HTML forms as the front end and Perl-cgi as back end. The HTML form has various fields that allows user to inout the data. Now, when the user enters his/her phone number in the phone field, the maximum digits sould be entered is 10. If user tries to enter 11th digit a pop up error message should be generated refraining the user to do so. Can I do this in Perl? Here is a sample code. Please help!! Cheers! Rocko19

Replies are listed 'Best First'.
Re: Create pop up error message in Perl
by almut (Canon) on Feb 11, 2009 at 07:55 UTC
    If user tries to enter 11th digit a pop up error message should be generated ... Can I do this in Perl?

    As Perl runs on the back end, you'd have to send a request to the server for every key stroke, otherwise how would the back end be supposed to know what the user has typed so far?   So, in short, I'd say: No, this can't practically be done with Perl1. (Use JavaScript instead.)

    ___

    1 Update: ...with Perl alone plus standard HTML forms, that is. Of course, you could use Ajax. (But think twice before you decide to do so - depending on client-server round trip times, it might not be the best idea to call a server-side function for every key stroke.  As you'll need client-side Javascript (or VBScript, etc.) anyway for the Ajax glue code, it's almost certainly better to implement such a simple task as checking the length of an input string, in that client-side scripting language right away...)


      Hi almut,

      If we take the the data user has entered in the phone field and then checks its format and if its greater then 10 digits can we throw an error message in Perl?

      Thanks

      Rocko19
Re: Create pop up error message in Perl
by hominid (Priest) on Feb 11, 2009 at 14:35 UTC
    CGI::Ajax may be helpful. Here are a couple of quick examples to play around with. First is an example without javascript that prints to the page instead of a popup. (Update: More accurately said as "without having to write any javascript yourself", thanks almut)
    #! /usr/bin/perl use strict; use warnings; use CGI; use CGI::Ajax; my $cgi = new CGI; my $pjx = new CGI::Ajax( 'exported_func' => \&too_many_nums ); print $pjx->build_html( $cgi, \&Show_HTML); sub too_many_nums { my $output = ""; my $input = shift; if( length($input) > 10 ) { $output = "Too many numbers!"; } return( $output ); } sub Show_HTML { my $html = <<EOHTML; <HTML> <BODY> Enter something: <input type="text" name="val1" id="val1" onkeyup="exported_fun +c( ['val1'], ['resultdiv'] );"> <br> <div id="resultdiv"></div> </BODY> </HTML> EOHTML return $html; }
    And here is an example that uses a javascript popup.
    #! /usr/bin/perl use strict; use warnings; use CGI; use CGI::Ajax; my $cgi = new CGI; my $pjx = new CGI::Ajax( 'exported_func' => \&too_many_nums ); print $pjx->build_html( $cgi, \&Show_HTML); sub too_many_nums { my $output = ""; my $input = shift; if( length($input) > 10 ) { $output = "Too many numbers!"; } return( $output ); } sub Show_HTML { my $html = <<EOHTML; <HTML> <BODY> <head> <script type="text/javascript"> function disp_alert() { var input = arguments[0] if( input != "" ) { alert(input); } } </script> </head> Enter something: <input type="text" name="val1" id="val1" onkeyup="exported_fun +c( ['val1'], [disp_alert] );"> <br> <div id="resultdiv"></div> </BODY> </HTML> EOHTML return $html; }
    hth
      First is an example without javascript

      I know what you mean... but strictly speaking, this is not without Javascript. CGI::Ajax uses Javascript code (auto-generated and embedded in the page) to glue the server-side Perl routines to the client-side events.  Also, onkeyup="..." generally wouldn't work without Javascript enabled.

      Although Ajax doesn't necessarily require Javascript, it always needs some dynamic/scripting environment client-side (alternatives to Javascript are for example VBScript or EGL).

Re: Create pop up error message in Perl
by Anonymous Monk on Feb 11, 2009 at 07:46 UTC
    If user tries to enter 11th digit a pop up error message should be generated refraining the user to do so. Can I do this in Perl?
    Yes, if you write a browser in perl ... or embed perl in a browser (like firefox) ... or use client-side javascript (or vbscript...). Here is sample code.
    print "\n\n<br><br><b> Phone Number (Please indicate whether this is a + business, home or cell number): &nbsp&nbsp </b>"; print "<input type=text size=25 maxlength=23 name = \"phone\" value = +\"$phone\" SOMETHINGSOMETHINGJAVASCRIPTGOESHERE>";