Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

From Validation via JavaScript 1

Status
Not open for further replies.

max1x

Programmer
Jan 12, 2005
366
0
0
US
I am not sure how to embed JavaScript "onSubmit" in cgi script without using the -src option and call an external .js script.

use CGI/:all/;

print header();
print start_html("Welcome to the OnDemand Tool",
-script=>{-language=>'JAVASCRIPT'});
print start_form(-action=>'addProjectData.cgi',-onSubmit=>'return validateForm(this)');

more CGI CODE to define fields...

This is not working for me.

print <<validateForm;
function validateForm(theForm) {
var why = "";
why += checkProjectName(theForm.projectName.value);
if (why != "") {
alert (why);
return false;
}
return true;
}
function checkProjectName (strng) {
var error ="";
if(strng == "") {
error = "-- You forgot to enter Project Name. \n";

}
return error;
}
</script>
validateForm

 
You are using the CGI.pm incorrectly.

This is how it should be done.
Code:
#!/usr/bin/perl -w

use strict;

use CGI qw/ :standard /;

my $jscript = <<validateForm;
function validateForm(theForm) {
    var why = "";
    why += checkProjectName(theForm.projectName.value);
    if (why != "") {
        alert (why);
        return false;
    }
    return true;
}

function checkProjectName(strng) {
    var error ="";
    if(strng == "") {
        error = "-- You forgot to enter Project Name. \n";
    }
    return error;
}
//->
validateForm

print header(),
      start_html(
          -title  => 'Welcome to the OnDemand Tool',
          -script => $jscript
      ),
      start_form(
          -action   => 'addProjectData.cgi',
          -onSubmit => 'return validateForm(this)'
      );

M. Brooks
 
Using the above code, I'm still getting Server Level validation vs Client.

print "<table width=\"50%\">";
print "<tr bgcolor=\"orange\"><td align=\"left\">";
print "<tr><td>Project Name:</td><td>";
print textfield('projectName','',10,15);
print "</td></tr><tr><td>";
print submit('Submit Project');
print "<td align=\"right\">";
print reset('Clear Form');
print "</td></tr>";
print "</table>";

print end_form;
print end_html;
 
The problem is the newline character in the javascript.
I made some changes to the script but it works.
Code:
#!/usr/bin/perl -w

use strict;

use CGI qw/ :standard /;

#!/usr/bin/perl -w

use strict;

use CGI qw/ :standard /;

my $jscript = <<validateForm;
function validateForm(theForm) {
    var why = "";
    why += checkProjectName(theForm.projectName.value);
    if (why != "") {
        alert (why);
        return false;
    }
    return true;
}

function checkProjectName(strng) {
    var error = "";
    if (strng == "") {
        error = "You forgot to enter Project Name.";      
    }
    return error;
}
//->
validateForm

print header(),
      start_html(
          -title  => 'Welcome to the OnDemand Tool',
          -script => $jscript
      ),
      start_form(
          -action   => 'addProjectData.cgi',
          -name     => 'theForm',
          -onSubmit => 'return validateForm(this)'
      );

print <<formInput;

<input type="text" name="projectName" value="">

<input type="submit" value="Submit">

formInput

print end_form(),
      end_html();

M. Brooks
 
This seems to have done the trick:

-name => 'theForm'

I left everything as is.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top