Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.1 KiB

  1. #!/usr/local/bin/perl
  2. # This script illustrates how to use JavaScript to validate fill-out
  3. # forms.
  4. use CGI qw(:standard);
  5. # Here's the javascript code that we include in the document.
  6. $JSCRIPT=<<EOF;
  7. // validate that the user is the right age. Return
  8. // false to prevent the form from being submitted.
  9. function validateForm() {
  10. var today = new Date();
  11. var birthday = validateDate(document.form1.birthdate);
  12. if (birthday == 0) {
  13. document.form1.birthdate.focus()
  14. document.form1.birthdate.select();
  15. return false;
  16. }
  17. var milliseconds = today.getTime()-birthday;
  18. var years = milliseconds/(1000 * 60 * 60 * 24 * 365.25);
  19. if ((years > 20) || (years < 5)) {
  20. alert("You must be between the ages of 5 and 20 to submit this form");
  21. document.form1.birthdate.focus();
  22. document.form1.birthdate.select();
  23. return false;
  24. }
  25. // Since we've calculated the age in years already,
  26. // we might as well send it up to our CGI script.
  27. document.form1.age.value=Math.floor(years);
  28. return true;
  29. }
  30. // make sure that the contents of the supplied
  31. // field contain a valid date.
  32. function validateDate(element) {
  33. var date = Date.parse(element.value);
  34. if (0 == date) {
  35. alert("Please enter date in format MMM DD, YY");
  36. element.focus();
  37. element.select();
  38. }
  39. return date;
  40. }
  41. // Compliments, compliments
  42. function doPraise(element) {
  43. if (element.checked) {
  44. self.status=element.value + " is an excellent choice!";
  45. return true;
  46. } else {
  47. return false;
  48. }
  49. }
  50. function checkColor(element) {
  51. var color = element.options[element.selectedIndex].text;
  52. if (color == "blonde") {
  53. if (confirm("Is it true that blondes have more fun?"))
  54. alert("Darn. That leaves me out.");
  55. } else
  56. alert(color + " is a fine choice!");
  57. }
  58. EOF
  59. ;
  60. # here's where the execution begins
  61. print header;
  62. print start_html(-title=>'Personal Profile',-script=>$JSCRIPT);
  63. print h1("Big Brother Wants to Know All About You"),
  64. strong("Note: "),"This page uses JavaScript and requires ",
  65. "Netscape 2.0 or higher to do anything special.";
  66. &print_prompt();
  67. print hr;
  68. &print_response() if param;
  69. print end_html;
  70. sub print_prompt {
  71. print start_form(-name=>'form1',
  72. -onSubmit=>"return validateForm()"),"\n";
  73. print "Birthdate (e.g. Jan 3, 1972): ",
  74. textfield(-name=>'birthdate',
  75. -onBlur=>"validateDate(this)"),"<p>\n";
  76. print "Sex: ",radio_group(-name=>'gender',
  77. -value=>[qw/male female/],
  78. -onClick=>"doPraise(this)"),"<p>\n";
  79. print "Hair color: ",popup_menu(-name=>'color',
  80. -value=>[qw/brunette blonde red gray/],
  81. -default=>'red',
  82. -onChange=>"checkColor(this)"),"<p>\n";
  83. print hidden(-name=>'age',-value=>0);
  84. print submit();
  85. print end_form;
  86. }
  87. sub print_response {
  88. import_names('Q');
  89. print h2("Your profile"),
  90. "You claim to be a ",b($Q::age)," year old ",b($Q::color,$Q::gender),".",
  91. "You should be ashamed of yourself for lying so ",
  92. "blatantly to big brother!",
  93. hr;
  94. }