Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP 3 Classic ASP Active Server Pages 3.0 > Classic ASP Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 8th, 2008, 08:50 AM
Authorized User
Points: 76, Level: 1
Points: 76, Level: 1 Points: 76, Level: 1 Points: 76, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2007
Location: , , .
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default JS to check for form Validation does not work - pl

Hello,

I have one question about including a JS file on my ASP page. Asp works fine. When I include a JS file, it does not do anything when I hit "Submit". It should check for empty fields.

Does it relate to onChange="javascript:frmSelect.submit(); on my ASP page?
If yes, how do I fix it to make my form validation work?

Thanks so much.

Here is my code:

<head>
<title>Example combo box</title>
<script language="JavaScript" type="text/javascript" src="inc/js.js"></script>
</head>
<form name="frmSelect" method="Post" action="selectcombo1.asp">
...
<SELECT name="cars" onChange="javascript:frmSelect.submit();">
...
<p><input name="Submit" type="submit" value="Submit"></p>
----------------------------------------------------------
js.js file:

var maxC=100;
function limC(obj){
while(obj.value.length>maxC){
obj.value=obj.value.replace(/.$/,'');
}
}

function chFrm() {
var digits = "0123456789"; // Email validation check
var chEmail = new RegExp("^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$") // Email validation check
var errs = 0
var msgbox = "You forgot the following question (s).\nPlease complete and re-submit. Thanks!\n";
var goon = msgbox
var ctr = 0;

var radioCk = false;
for (i = 0; i < document.frmSelect.Q1.length; i++) {
if (document.frmSelect.Q1[i].checked)
radioCk = true; }
if (!radioCk) {
msgbox = msgbox + "\n Question #1 - Please check one box";
}

if (document.frmSelect.Q3.value=="") {
msgbox = msgbox + "\n Question #3 - Please add your comments";
}

// If no errors found, then go ahead to submit the form
if (msgbox == goon) {
return true;
} else {

alert(msgbox);
return false;
}
}


---------------------
There is NO stupid question.
__________________
---------------------
There is NO stupid question.
Reply With Quote
  #2 (permalink)  
Old December 11th, 2008, 06:39 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

You have NO CODE AT ALL in there that would invoke any validation of any kind.

And the validation code you show seems to have nothing at all to do with the <FORM> that you show.

In fact, the JS code you show has two major functionalities code that are never used at all, no matter what.

Having said that: Even if you had proper validation code and a proper call to it, indeed calling submit() from the onChange even would bypass it.
Reply With Quote
  #3 (permalink)  
Old December 11th, 2008, 09:01 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

To elaborate on what Pedant has said: by wiring up the onChange event of your SELECT box to submit the form, the form is automatically being sent back to the server thus bypassing any validation routines.

What you might do is something like this

JavaScript Code:
function DoSomthingForSelects()
{
     //Do Something
}

function ValidateForm()
{
   //Do validation routines
  return true;
}

HTML Code:
<head>
<title>Example combo box</title>
</head>
<form name="frmSelect" method="Post" action="selectcombo1.asp">
...
<SELECT name="cars" onChange="DoSomthingForSelects()">
...
<p><input name="Submit" type="submit" onClick="return ValidateForm()"  value="Submit"></p>

By calling a JavaScript method (instead of submitting the form onChange) you can do whatever it is you need to do in that method. When the user clicks the submit button your validation routines are called and the form is submitted.***

***If you validation routine were to fail your function should return false which will prevent the form from being submitted so something like this:


JavaScript Code:
function ValidateForm()
{
   if(SomeCondition == false) { return false; }
   if(SomeOtherCondition == false) { return false; }
  return true;
}

As with any post asking about JavaScript validation I need to make the comment that if you are going to go to the trouble to validate the data on the client side, you should also go to the trouble to validate the data on the server side since shutting of JavaScript in a browser is trivial.


hth.
Doug
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
Reply With Quote
  #4 (permalink)  
Old December 11th, 2008, 11:13 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

As a very minor comment on Doug's code:

It's quite likely that don't really need his DoSomethingForSelects function, actually.

You can probably just code
Code:
<SELECT name="cars" onChange="if ( validateForm ) this.form.submit();">
Reply With Quote
  #5 (permalink)  
Old December 11th, 2008, 11:18 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Quite right. It will, however, depend on the desired functionality. Typically when I see someone posting a form based on the onChanged event of a SELECT box it is because they need some sort of data back from the server before the user can continue to the next step of the form. By using the DoSomething method you can integrate some AJAX (or other functionality as necessary) to continue normal operation of the form without needing to go back to the server.

-Doug
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
js validation for radiobuttonlist sureshdev786 C# 1 October 24th, 2005 02:02 AM
JS validation on the drop-down menus crmpicco Javascript How-To 0 July 1st, 2005 05:30 AM
How to stop user from view js code but still work bekim Javascript How-To 4 January 7th, 2005 09:08 PM
checkboxes check is it possible using js blueguy Javascript 1 October 26th, 2004 02:35 AM



All times are GMT -4. The time now is 01:01 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.