PDA

View Full Version : OnSubmit Woes... I'm new to JS


azucker1
10-09-2002, 10:03 PM
I'm new to JS and am trying to get two scripts to run when a form is submitted. One makes sure fields are filled in, one makes sure some fields are only numbers. I can get one or the other to work, but when I try to combine them in the form onsubmit, only the first one list works. I've tried to do as some of the online help suggests, combine them in the body tag, but then neither works. (I'm guessing that's b/c it has to do with the form, not the body of the page.) Any help is more than appreciated being that I'm running out of ideas.

Thanks for the assist,
Adam

Where I found the scripts:
Number Check: http://javascriptkit.com/script/script2/numc.shtml
Required Fields: http://navsurf.com/dhtml/formcheck.asp

Here is my current form line:
<form name="JobNumberRequest" method="post" action="jaob_number_request_confirm.cfm" onsubmit="return formCheck(this);return checkban()">

Here are the scripts:
<script language="JavaScript1.2">
/*Number check script-
By JavaScript Kit (www.javascriptkit.com)
Over 200 free scripts here!
*/

function checknumber(){
var x=document.JobNumberRequest.RequiredEstTotalFee.value
var anum=/(^\d+$)|(^\d+\.\d+$)/
if (anum.test(x))
testresult=true
else{
alert("Please input a valid number!")
testresult=false
}
return (testresult)
}

</script>
<script>
function checkban(){
if (document.layers||document.all||document.getElementById)
return checknumber()
else
return true
}
</script>
<script language="JavaScript">
<!--
// Copyright information must stay intact
// FormCheck v1.02
// Copyright NavSurf.com 2002, all rights reserved
// For more scripts, visit NavSurf.com at http://navsurf.com

function formCheck(formobj){
// name of mandatory fields
var fieldRequired = Array("requiredclientname", "requiredprojectname", "requiredLGNOffice", "requiredProjManager", "requiredProjectType", "RequiredEstTotalFee");
// field description to appear in the dialog box
var fieldDescription = Array("Client Name", "Project Name", "Profit Center", "Project Manager", "Project Type", "Est. Total Fee");
// dialog message
var alertMsg = "Please complete the following fields:\n";

var l_Msg = alertMsg.length;

for (var i = 0; i < fieldRequired.length; i++){
var obj = formobj.elements[fieldRequired[i]];
if (obj){
switch(obj.type){
case "select-one":
if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
case "select-multiple":
if (obj.selectedIndex == -1){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
case "text":
case "textarea":
if (obj.value == "" || obj.value == null){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
default:
if (obj.value == "" || obj.value == null){
alertMsg += " - " + fieldDescription[i] + "\n";
}
}
}
}

if (alertMsg.length == l_Msg){
return true;
}else{
alert(alertMsg);
return false;
}
}
// -->
</script>

Roy Sinclair
10-09-2002, 10:11 PM
This onsubmit="return formCheck(this);return checkban()" is the problem. The return for formCheck terminates the processing. This onsubmit="return ( formCheck(this) && checkban() );" should work.

beetle
10-09-2002, 10:12 PM
Trash 'em both and use my validator, fValidate (http://www.peterbailey.net/fValidate)

I can provide you with any assitance you need to get it up and running (that is, if you have problems with it in the first place...it's incredibly easy to use...)

azucker1
10-09-2002, 10:14 PM
WOW! You guys are fast (and great too.) Roy, the new line works great. THANK YOU!! Beetle, I'm going to check out your link.

-Adam:thumbsup:

azucker1
10-09-2002, 10:19 PM
If I don't switch to Beetle's stuff (which looks good), is it possible to have the number script check mulitple fields?

-Adam

<script language="JavaScript1.2">
/*Number check script-
By JavaScript Kit (www.javascriptkit.com)
Over 200 free scripts here!
*/

function checknumber(){
var x=document.JobNumberRequest.EstConsultantFee.value
var anum=/(^\d+$)|(^\d+\.\d+$)/
if (anum.test(x))
testresult=true
else{
alert("Please input a valid number!")
testresult=false
}
return (testresult)
}

</script>

beetle
10-09-2002, 10:32 PM
I wrote fValidate specifically to circumvent the types of problems you are having, azucker1. Use it. Trust me, you'll save TONS of time.

azucker1
10-09-2002, 10:40 PM
Do you have AIM? If so, drop me a line.

-Adam

Roy Sinclair
10-10-2002, 02:57 PM
Originally posted by azucker1
If I don't switch to Beetle's stuff (which looks good), is it possible to have the number script check mulitple fields?

-Adam

<script language="JavaScript1.2">
/*Number check script-
By JavaScript Kit (www.javascriptkit.com)
Over 200 free scripts here!
*/

function checknumber(){
var x=document.JobNumberRequest.EstConsultantFee.value
var anum=/(^\d+$)|(^\d+\.\d+$)/
if (anum.test(x))
testresult=true
else{
alert("Please input a valid number!")
testresult=false
}
return (testresult)
}

</script>

Even if you switch to Beetle's stuff, you should learn how to make this kind of change to a function.


function checknumber(fmField){
var x=fmField.value
var anum=/(^\d+$)|(^\d+\.\d+$)/
if (anum.test(x))
testresult=true
else{
alert("Please input a valid number!")
testresult=false
}
return (testresult)
}


The simple changes I made above turned that checknumber function into one which could be use for any text field. The only other difference is that when you call it you have to provide the reference to the form field instead of expecting the function to "know" it like it did when it was hard coded.

azucker1
10-17-2002, 07:34 PM
Peter, how would I structure the alt to accept only numbers, (blank fields ok) that can can have both decimals and commas?

Thanks,
Adam

beetle
10-17-2002, 09:28 PM
alt="custom|bok" pattern="^[\d,.]$"

azucker1
11-07-2002, 03:37 PM
Beetle, the custom alt didn't work. I've treid a combination of numbers (with different commas and decimals) and it didn't work. I need the field to accept the following formats, any ideas?

-Adam

Formats:
111,111.00
111.00
111,111
111111
111111.00
etc...

beetle
11-07-2002, 03:48 PM
Oops...sorry, I was missing a quantifier...

alt="custom|bok" pattern="^[\d,.]*$"