473,396 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

[baffled] - Javascript causes page to reload

I'm building a suite of online forms for insurance. These have been
stripped down from messy MS Word templates, and two of the six,
substantially identical, are misbehaving with the .js page that validates
some of the content.

The correct behaviour is that some fields must have content, some must
have valid dates/times. On validation, the form is POSTed to a processing
page that flows it into a blank template and emails that elsewhere.

The two that go wrong detect the empty/wrong fields, pop the alert window
up, but instead of the correct behaviour, to move the page focus to the
empty field and wait, the page is reloaded, with what appears to be a GET
command - the address window displays the URL as:
http://xxxx.xxx.xx/zsf/PLhouse.asp?t...e=PL&intform=6
&Claimno=&IncidentDepartmentCd=&IncidentDepartment =&CustomerRef=&Incident
Date=&IncidentTime=&COBAddress0=&COBAddress1=&COBA ddress2=&COBAddress3
=&COBAddress4
=&COBPostCode=&PLNatureOfLossCode=&PLNatureOfLoss= &PLCauseCode=&PLCause=&
PLOccupationCode=&PLOccupation=&PLEmpStatusCode=&P LEmpStatus=&AccidentOcc
urred=&ContractorsInvolved=&ContractorsNatureInv=& ContractorsInsurers=&Cl
aimNo2
=&ClaimantRef=&ClaimantTitle=&ClaimantInitials=&Cl aimantName=&ClaimantAge
=&ClaimantNINo=&ClaimantAddress0=&ClaimantAddress1 =&ClaimantAddress2
=&ClaimantAddress3=&ClaimantAddress4
=&ClaimantPostCode=&ClaimantOccupation=&HospitalDe tails=&ClaimantInsurers
=&ClaimOralOrWritten=&ClaimantDateCeasedWork=&Clai mantDateResumedWork=&Cl
aimantDateLeftWork=&SolicitorDetails=&SolicitorAdd ress0
=&SolicitorAddress1=&SolicitorAddress2=&SolicitorA ddress3
=&SolicitorAddress4
=&SolicitorPostCode=&SolicitorRef=&ClaimantInjuryC ode=&ClaimantInjury=&Cl
aimantDamageCode=&ClaimantDamage=&COBTypeOfPremise sCd=&COBTypeOfPremises=
&COBLocationCode=&COBLocation=&ClaimNo3
=&InspectionLastDate=&InspectionBy=&InspectionCond ition=&ComplaintsPrior=
&ComplaintsMadeBy=&ComplaintsMadeTo=&ComplaintsAct ion=&RecordOfComplaints
=&ComplaintRecorded=&PropClaimSeen=&PropClaimConfi rm=&Witness1Name=&Witne
ss1Address1=&ClaimNo4
=&CustomerCodeHOU=&CustomerCodeBLD=&CustomerCodeCL G=&CustomerCodeCMS=&Cus
tomerCodeEVS=&CustomerCodeCPS=&CustomerCodeGDM=&Cu stomerCodeHHS=&Customer
CodePGD=&CustomerCodePPY=&CustomerCodeCTG=&Custome rCodeGML=&CustomerCodeM
AG=&CustomerCodePKO=&CustomerCodePCT=&CustomerCode BUC=&CustomerCodeRLS=&C
ustomerCodeSIS=&CustomerCodeETN=&CustomerCodeRSE=& CustomerCodeVEH=&Custom
erCodeWTD=&CustomerCodeFDT=&CustomerCodePOL=&Custo merCodeITS=&CustomerCod
eERD=&CustomerCodeSPH=&CustomerCodeAIR=&CustomerCo deOTH=&ClaimNo5
=&Signature=&SignedDate=&Designation=&AdditionalIn fo=
This is what's got me baffled. Why would it do that? The .js page is
below

'''''''''''''''''''''''''''''''''''''''''''''''''' '

function form_confirm (Formname){

var probflag = false;
// IncidentDate - must have valid format and be filled in
var dt = document.forms[Formname].IncidentDate.value;
dt = dt.replace(/^[\s]+/g,''); // leading whitespace -> null
dt = dt.replace(/[\s]+$/g,''); // trailing whitespace -> null
if (dt == "") {
alert("Incident Date must be filled in");
document.forms[Formname].IncidentDate.focus();
probflag=true;
return false;
}
udt = dt;
if(udt.indexOf("/") == -1){
alert('Not a valid date, format '+dtFormat);
document.forms[Formname].IncidentDate.focus();
return false;
}
dt1 = udt.split("/")
dd1 = parseInt(dt1[0]);
mm1 = parseInt(dt1[1]);
yy1 = parseInt(dt1[2]);
//if(isNaN(dd1) || isNaN(mm1) || isNaN(yy1)){
if(isNaN(dd1) || isNaN(yy1)){
alert('Invalid Date for Incident Date !');
document.forms[Formname].IncidentDate.focus();
return false;
}
// IncidentTime
var dt = document.forms[Formname].IncidentTime.value;
dt = dt.replace(/^[\s]+/g,''); // leading whitespace -> null
dt = dt.replace(/[\s]+$/g,''); // trailing whitespace -> null
if (dt == "") {
alert("Incident Time must be filled in");
document.forms[Formname].IncidentTime.focus();
probflag=true;
return false;
}
udt = dt;
dtFormat = "HH:MM";
if(udt.indexOf(":") == -1){
alert('Not a valid time - must be in format '+dtFormat);
document.forms[Formname].IncidentTime.focus();
return false;
}
dt1 = udt.split(":")
hh1 = parseInt(dt1[0]);
mm1 = parseInt(dt1[1]);
//yy1 = parseInt(dt1[2]);
//if(isNaN(dd1) || isNaN(mm1) || isNaN(yy1)){
if(isNaN(hh1) || isNaN(mm1)){
alert('Invalid Time for Incident Time !');
document.forms[Formname].IncidentTime.focus();
return false;
}

// COBAddress1 - street of address where loss occured must be filled
in
var dt = document.forms[Formname].COBAddress1.value;
dt = dt.replace(/^[\s]+/g,''); // leading whitespace -> null
dt = dt.replace(/[\s]+$/g,''); // trailing whitespace -> null
if (dt == "") {
alert("Address must be filled in");
document.forms[Formname].COBAddress1.focus();
probflag=true;
return false;
}
// ClaimantName - not for 'property' form
if (Formname != 'PR')
{

var dt = document.forms[Formname].ClaimantName.value;
dt = dt.replace(/^[\s]+/g,''); // leading whitespace -> null
dt = dt.replace(/[\s]+$/g,''); // trailing whitespace -> null
if (dt == "") {
alert("Claimant Name must be filled in");
document.forms[Formname].ClaimantName.focus();
probflag=true;
return false;
}
}
// IncidentDepartment - department concerned must be filled in
//var dt = document.forms[Formname].IncidentDepartment.value;
//dt = dt.replace(/^[\s]+/g,''); // leading whitespace -> null
//dt = dt.replace(/[\s]+$/g,''); // trailing whitespace -> null
//alert ("check box 1 [" +document.forms
[Formname].CheckHOU.checked+ "]" );
if (document.forms[Formname].CheckHOU.checked == false &&
document.forms[Formname].CheckBLD.checked == false &&
document.forms[Formname].CheckCLG.checked == false &&
document.forms[Formname].CheckCMS.checked == false
&&
document.forms[Formname].CheckEVS.checked == false &&
document.forms[Formname].CheckCPS.checked == false
&& document.forms[Formname].CheckGDM.checked == false &&
document.forms[Formname].CheckHHS.checked == false
&& document.forms[Formname].CheckPGD.checked == false &&
document.forms[Formname].CheckPPY.checked == false
&& document.forms[Formname].CheckCTG.checked == false &&
document.forms[Formname].CheckGML.checked == false
&& document.forms[Formname].CheckMAG.checked == false &&
document.forms[Formname].CheckPKO.checked == false
&& document.forms[Formname].CheckPCT.checked == false &&
document.forms[Formname].CheckBUC.checked == false
&& document.forms[Formname].CheckRLS.checked == false &&
document.forms[Formname].CheckSIS.checked == false
&& document.forms[Formname].CheckETN.checked == false &&
document.forms[Formname].CheckRSE.checked == false
&& document.forms[Formname].CheckVEH.checked == false &&
document.forms[Formname].CheckWTD.checked == false
&& document.forms[Formname].CheckFDT.checked == false &&
document.forms[Formname].CheckPOL.checked == false
&& document.forms[Formname].CheckITS.checked == false &&
document.forms[Formname].CheckERD.checked == false
&& document.forms[Formname].CheckSPH.checked == false &&
document.forms[Formname].CheckAIR.checked == false
&& document.forms[Formname].CheckOTH.checked == false)
{
alert("Department must be filled in");
document.forms[Formname].CheckHOU.focus();
probflag=true;
return false;
}
//if (Formname != 'MO')
//{
// if (document.forms[Formname].CheckFTD.checked == false)
// {
// alert("Department must be filled in");
//document.forms[Formname].CheckHOU.focus();
//probflag=true;
//return false;
// }
//}
// Signature - Signature must be filled in
var dt = document.forms[Formname].Signature.value;
dt = dt.replace(/^[\s]+/g,''); // leading whitespace -> null
dt = dt.replace(/[\s]+$/g,''); // trailing whitespace -> null
if (dt == "") {
alert("Signature must be filled in");
document.forms[Formname].Signature.focus();
probflag=true;
return false;
}

// Designation - Designation must be filled in
var dt = document.forms[Formname].Designation.value;
dt = dt.replace(/^[\s]+/g,''); // leading whitespace -> null
dt = dt.replace(/[\s]+$/g,''); // trailing whitespace -> null
if (dt == "") {
alert("Designation must be filled in");
document.forms[Formname].Designation.focus();
probflag=true;
return false;
}

// SignedDate - must have valid format and be filled in
var dt = document.forms[Formname].SignedDate.value;
dt = dt.replace(/^[\s]+/g,''); // leading whitespace -> null
dt = dt.replace(/[\s]+$/g,''); // trailing whitespace -> null
if (dt == "") {
alert("Completed Date should be entered");
document.forms[Formname].SignedDate.focus();
probflag=true;
return false;
}
udt = dt;
if(udt.indexOf("/") == -1){
alert('Not a valid date, format '+dtFormat);
document.forms[Formname].SignedDate.focus();
return false;
}
dt1 = udt.split("/")
dd1 = parseInt(dt1[0]);
mm1 = parseInt(dt1[1]);
yy1 = parseInt(dt1[2]);
//if(isNaN(dd1) || isNaN(mm1) || isNaN(yy1)){
if(isNaN(dd1) || isNaN(yy1)){
alert('Invalid Date for Signed Date !');
document.forms[Formname].SignedDate.focus();
return false;
}
//if (probflag = false && (confirm('Are you sure you want to submit
this form?')))
if (confirm('Are you sure you want to submit this form?')){
if (probflag==false)
{
NewWindow('','zurich_form_qfp','650','400','yes');
document.forms[Formname].method="POST"
document.forms[Formname].action="zurich_forms.asp"
document.forms[Formname].target="zurich_form_qfp"
document.forms[Formname].submit();
}
}
}
''''''''''''''''''''''''''''''''''''''''''''''
Jul 23 '05 #1
13 2383
"s_m_b" <sm********@hotmail.com> wrote in
news:Xn**********************************@216.196. 97.138:
I'm building a suite of online forms for insurance. These have been
stripped down from messy MS Word templates, and two of the six,
substantially identical, are misbehaving with the .js page that
validates some of the content.


should add......

the script is called with
<input type = "submit" value = "submit" onclick="form_confirm ('xx');" />
where 'xx' is the name of the form itself.
IE is the browser causing the variable behaviour, but Firefox consistantly
forces the page reload, for all the forms.
Jul 23 '05 #2
s_m_b wrote on 10 feb 2005 in comp.lang.javascript:
the script is called with
<input type = "submit" value = "submit" onclick="form_confirm ('xx');"
/> where 'xx' is the name of the form itself.
IE is the browser causing the variable behaviour, but Firefox
consistantly forces the page reload, for all the forms.


onclick="return form_confirm('xx');"


--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #3
"Evertjan." <ex**************@interxnl.net> wrote in
news:Xn*******************@194.109.133.29:

no - no different
I've slightly changed the js file so that the last bit reads now as:

if (probflag==false)
{
if (confirm('Are you sure you want to submit this form?'))
{
//if (probflag==false)
{
NewWindow('','zurich_form_qfp','650','400','yes');
document.forms[Formname].method="POST"
document.forms[Formname].action="zurich_forms.asp"
document.forms[Formname].target="zurich_form_qfp"
document.forms[Formname].submit();
}
}
}

from testing with an alert box, the 'testing' function that fails its
content doesn't seem to use the 'return false;'

s_m_b wrote on 10 feb 2005 in comp.lang.javascript:
the script is called with
<input type = "submit" value = "submit" onclick="form_confirm ('xx');"
/> where 'xx' is the name of the form itself.
IE is the browser causing the variable behaviour, but Firefox
consistantly forces the page reload, for all the forms.


onclick="return form_confirm('xx');"


Jul 23 '05 #4
Lee
s_m_b said:

"s_m_b" <sm********@hotmail.com> wrote in
news:Xn**********************************@216.196 .97.138:
I'm building a suite of online forms for insurance. These have been
stripped down from messy MS Word templates, and two of the six,
substantially identical, are misbehaving with the .js page that
validates some of the content.


should add......

the script is called with
<input type = "submit" value = "submit" onclick="form_confirm ('xx');" />
where 'xx' is the name of the form itself.
IE is the browser causing the variable behaviour, but Firefox consistantly
forces the page reload, for all the forms.


Firefox is doing what you're telling it to do.
When you click on an input of type "submit", it submits the form
regardless of what may happen in the onclick handler.
In this case, that causes the reload.

If you only want the form to be submitted after passing some audit,
you define an onSubmit event handler (never onClick) and have it
return false if the form should NOT be submitted.

Jul 23 '05 #5
Lee <RE**************@cox.net> wrote in
news:cu*********@drn.newsguy.com:

ah![blush] should have thought of that.... mind you, the pages were
built a while back and I've learned a bit since then. Just missed the
blinding obvious

Begs the question ?why does IE allow what wouldn't normally work to work?
OK, FF is far better on behaviours with JS, but never gives out any
runtime errors - I'd far rather use FF to test and debug, but IE always
gives you the JS error ... grumble grumble
s_m_b said:

"s_m_b" <sm********@hotmail.com> wrote in
news:Xn**********************************@216.19 6.97.138:
I'm building a suite of online forms for insurance. These have been
stripped down from messy MS Word templates, and two of the six,
substantially identical, are misbehaving with the .js page that
validates some of the content.


should add......

the script is called with
<input type = "submit" value = "submit" onclick="form_confirm ('xx');"
/> where 'xx' is the name of the form itself.
IE is the browser causing the variable behaviour, but Firefox
consistantly forces the page reload, for all the forms.


Firefox is doing what you're telling it to do.
When you click on an input of type "submit", it submits the form
regardless of what may happen in the onclick handler.
In this case, that causes the reload.

If you only want the form to be submitted after passing some audit,
you define an onSubmit event handler (never onClick) and have it
return false if the form should NOT be submitted.


Jul 23 '05 #6
Lee wrote:

(snip)
Firefox is doing what you're telling it to do.
When you click on an input of type "submit", it submits the form
regardless of what may happen in the onclick handler.
In this case, that causes the reload.


(snip)

Not here (NS 7.2)...

Jul 23 '05 #7
s_m_b wrote:
Lee <RE**************@cox.net> wrote in
news:cu*********@drn.newsguy.com:

ah![blush] should have thought of that.... mind you, the pages were
built a while back and I've learned a bit since then. Just missed the blinding obvious

Begs the question ?why does IE allow what wouldn't normally work to work? OK, FF is far better on behaviours with JS, but never gives out any
runtime errors - I'd far rather use FF to test and debug, but IE always gives you the JS error ... grumble grumble
s_m_b said:

"s_m_b" <sm********@hotmail.com> wrote in
news:Xn**********************************@216.19 6.97.138:

I'm building a suite of online forms for insurance. These have been stripped down from messy MS Word templates, and two of the six,
substantially identical, are misbehaving with the .js page that
validates some of the content.
should add......

the script is called with
<input type = "submit" value = "submit" onclick="form_confirm ('xx');"/> where 'xx' is the name of the form itself.
IE is the browser causing the variable behaviour, but Firefox
consistantly forces the page reload, for all the forms.


Firefox is doing what you're telling it to do.
When you click on an input of type "submit", it submits the form
regardless of what may happen in the onclick handler.
In this case, that causes the reload.

If you only want the form to be submitted after passing some audit,
you define an onSubmit event handler (never onClick) and have it
return false if the form should NOT be submitted.


Suggestion: you could slim down that validator quite a bit. If you pass
the Form object to the function, you can put it in a variable and
reference that, instead of 'document.forms[Formname]' (which requires a
longer lookup) over and over:

....onclick="return form_confirm(this.form)">

function form_confirm (f){
var els = f.elements;

Now you can refer to the Form as 'f', and an element as simply, e.g.,
'els.IncidentDate'. Some looping wouldn't hurt either:

var cbs =
[
'HOU','BLD','CLG','CMS','EVS','CPS','GDM',
'HHS','PGD','PPY','CTG','GML','MAG','PKO',
'PCT','BUC','RLS','SIS','ETN','RSE','VEH',
'WTD','FDT','POL','ITS','ERD','SPH','AIR',
'OTH'
];

for (var i = 0, len = cbs.length; i < len; ++i)
if (els['Check' + cbs[i]].checked) //build name, plug in, checked?
break; //yes, break loop
if (i == len) //loop ran fully, none checked
{
alert("Department must be filled in");
els.CheckHOU.focus();
probflag = true;
return false;
}

Just an example. And...

dt = dt.replace(/^[\s]+/g,''); // leading whitespace -> null
dt = dt.replace(/[\s]+$/g,''); // trailing whitespace -> null

Make that:

function trim(s)
{
return s.replace(/^\s+/g,'').replace(/\s+$/g,'');
}
........
........
dt = trim(dt);

Etc.

Jul 23 '05 #8
"RobB" <fe******@hotmail.com> wrote in news:1108055032.573364.259770
@z14g2000cwz.googlegroups.com:

meaning?

Have to say I haven't looked at NS since v6 something, or Opera since v5.
both seem to loose the plot a bit.

Lee wrote:

(snip)
Firefox is doing what you're telling it to do.
When you click on an input of type "submit", it submits the form
regardless of what may happen in the onclick handler.
In this case, that causes the reload.


(snip)

Not here (NS 7.2)...


Jul 23 '05 #9
s_m_b wrote:
"RobB" <fe******@hotmail.com> wrote in news:1108055032.573364.259770
@z14g2000cwz.googlegroups.com:

meaning?

Have to say I haven't looked at NS since v6 something, or Opera since v5. both seem to loose the plot a bit.

Lee wrote:

(snip)
Firefox is doing what you're telling it to do.
When you click on an input of type "submit", it submits the form
regardless of what may happen in the onclick handler.
In this case, that causes the reload.


(snip)

Not here (NS 7.2)...


Don't have FF available at the moment - using a notebook - but NS 7 is
pure mozilla/gecko, and returning false to the onclick handler of a
control with a default action - like a type="submit" - has always
cancelled that action afaik. Not recommending it, you should, for
consistency, always go:

<form....onsubmit="return form_confirm(this)">

....but nevertheless I demur on the earlier point. Looking over your
original post for that unwanted submission issue...

Jul 23 '05 #10
Found one logic error -

You're doing this:

.........
udt = dt;
if(udt.indexOf("/") == -1){
alert('Not a valid date, format '+dtFormat);
.........

before you've defined 'dtFormat', the date template. It's specified
below that:

.........
udt = dt;
dtFormat = "HH:MM";
if(udt.indexOf(":") == -1){
........

Any error will cause the validator to fail, submitting the form. Nearly
impossible to test without the fields supplied (too many to type in !).

Jul 23 '05 #11
Lee wrote:
s_m_b said:

"s_m_b" <sm********@hotmail.com> wrote in
news:Xn**********************************@216.196 .97.138:
I'm building a suite of online forms for insurance. These have been stripped down from messy MS Word templates, and two of the six,
substantially identical, are misbehaving with the .js page that
validates some of the content.


should add......

the script is called with
<input type = "submit" value = "submit" onclick="form_confirm ('xx');" />where 'xx' is the name of the form itself.
IE is the browser causing the variable behaviour, but Firefox consistantlyforces the page reload, for all the forms.


Firefox is doing what you're telling it to do.
When you click on an input of type "submit", it submits the form
regardless of what may happen in the onclick handler.
In this case, that causes the reload.

If you only want the form to be submitted after passing some audit,
you define an onSubmit event handler (never onClick) and have it
return false if the form should NOT be submitted.


My apologies to Lee...\:=o

https://bugzilla.mozilla.org/show_bug.cgi?id=239295

Jul 23 '05 #12
JRS: In article <Xn**********************************@216.196.97.1 38>,
dated Thu, 10 Feb 2005 09:05:45, seen in news:comp.lang.javascript,
s_m_b <sm********@hotmail.com> posted :
// IncidentDate - must have valid format and be filled in
var dt = document.forms[Formname].IncidentDate.value;
dt = dt.replace(/^[\s]+/g,''); // leading whitespace -> null
dt = dt.replace(/[\s]+$/g,''); // trailing whitespace -> null
if (dt == "") {
alert("Incident Date must be filled in");
document.forms[Formname].IncidentDate.focus();
probflag=true;
return false;
}
udt = dt;
if(udt.indexOf("/") == -1){
alert('Not a valid date, format '+dtFormat);
document.forms[Formname].IncidentDate.focus();
return false;
}
dt1 = udt.split("/")
dd1 = parseInt(dt1[0]);
mm1 = parseInt(dt1[1]);
yy1 = parseInt(dt1[2]);
//if(isNaN(dd1) || isNaN(mm1) || isNaN(yy1)){
if(isNaN(dd1) || isNaN(yy1)){
alert('Invalid Date for Incident Date !');
document.forms[Formname].IncidentDate.focus();
return false;
}
Numeric dates can be validated better and more briefly. See FAQ & sig.

Function parseInt(S) will accept a large variety of undesirable strings.

// IncidentTime
var dt = document.forms[Formname].IncidentTime.value;
dt = dt.replace(/^[\s]+/g,''); // leading whitespace -> null
dt = dt.replace(/[\s]+$/g,''); // trailing whitespace -> null
if (dt == "") {
alert("Incident Time must be filled in");
document.forms[Formname].IncidentTime.focus();
probflag=true;
return false;
}
udt = dt;
dtFormat = "HH:MM";
if(udt.indexOf(":") == -1){
alert('Not a valid time - must be in format '+dtFormat);
document.forms[Formname].IncidentTime.focus();
return false;
}
dt1 = udt.split(":")
hh1 = parseInt(dt1[0]);
mm1 = parseInt(dt1[1]);
//yy1 = parseInt(dt1[2]);
//if(isNaN(dd1) || isNaN(mm1) || isNaN(yy1)){
if(isNaN(hh1) || isNaN(mm1)){
alert('Invalid Time for Incident Time !');
document.forms[Formname].IncidentTime.focus();
return false;
}
Likewise for times. Since you are not checking the values and will
accept 33:77,
OK = /^\s*\d\d:\d\d\s*$/.test(dt)

// IncidentDepartment - department concerned must be filled in
...
&& document.forms[Formname].CheckRLS.checked == false &&
document.forms[Formname].CheckSIS.checked == false
...
Consider having a default "Banda" department with a checked hidden
checkbox. You only need to check that Banda is clear to see that one of
the others is set.

// SignedDate - must have valid format and be filled in
var dt = document.forms[Formname].SignedDate.value;
...
Repeating code is always silly; use a function.

if (probflag==false)


Use if (!probflag)
Read <URL:http://www.merlyn.demon.co.uk/js-valid.htm> etc.

Keep the day job.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #13
Dr John Stockton <sp**@merlyn.demon.co.uk> wrote in
news:Ii**************@merlyn.demon.co.uk:

always delighted (:P) to hear from those who want to tell the world they
know better. As it stands, the code works fine. Not looking for 'better'
or 'less silly'.
Thanks to those of you with useful comments - client-side code isn't a
strong point for me.

JRS: In article <Xn**********************************@216.196.97.1 38>,
dated Thu, 10 Feb 2005 09:05:45, seen in news:comp.lang.javascript,
s_m_b <sm********@hotmail.com> posted :
// IncidentDate - must have valid format and be filled in
var dt = document.forms[Formname].IncidentDate.value;
dt = dt.replace(/^[\s]+/g,''); // leading whitespace -> null
dt = dt.replace(/[\s]+$/g,''); // trailing whitespace -> null
if (dt == "") {
alert("Incident Date must be filled in");
document.forms[Formname].IncidentDate.focus();
probflag=true;
return false;
}
udt = dt;
if(udt.indexOf("/") == -1){
alert('Not a valid date, format '+dtFormat);
document.forms[Formname].IncidentDate.focus();
return false;
}
dt1 = udt.split("/")
dd1 = parseInt(dt1[0]);
mm1 = parseInt(dt1[1]);
yy1 = parseInt(dt1[2]);
//if(isNaN(dd1) || isNaN(mm1) || isNaN(yy1)){
if(isNaN(dd1) || isNaN(yy1)){
alert('Invalid Date for Incident Date !');
document.forms[Formname].IncidentDate.focus();
return false;
}
Numeric dates can be validated better and more briefly. See FAQ & sig.

Function parseInt(S) will accept a large variety of undesirable

strings.
// IncidentTime
var dt = document.forms[Formname].IncidentTime.value;
dt = dt.replace(/^[\s]+/g,''); // leading whitespace -> null
dt = dt.replace(/[\s]+$/g,''); // trailing whitespace -> null
if (dt == "") {
alert("Incident Time must be filled in");
document.forms[Formname].IncidentTime.focus();
probflag=true;
return false;
}
udt = dt;
dtFormat = "HH:MM";
if(udt.indexOf(":") == -1){
alert('Not a valid time - must be in format '+dtFormat);
document.forms[Formname].IncidentTime.focus();
return false;
}
dt1 = udt.split(":")
hh1 = parseInt(dt1[0]);
mm1 = parseInt(dt1[1]);
//yy1 = parseInt(dt1[2]);
//if(isNaN(dd1) || isNaN(mm1) || isNaN(yy1)){
if(isNaN(hh1) || isNaN(mm1)){
alert('Invalid Time for Incident Time !');
document.forms[Formname].IncidentTime.focus();
return false;
}
Likewise for times. Since you are not checking the values and will
accept 33:77,
OK = /^\s*\d\d:\d\d\s*$/.test(dt)

// IncidentDepartment - department concerned must be filled in
...
&& document.forms[Formname].CheckRLS.checked == false && document.forms[Formname].CheckSIS.checked == false
...


Consider having a default "Banda" department with a checked hidden
checkbox. You only need to check that Banda is clear to see that one

of the others is set.

// SignedDate - must have valid format and be filled in
var dt = document.forms[Formname].SignedDate.value;
...


Repeating code is always silly; use a function.

if (probflag==false)


Use if (!probflag)
Read <URL:http://www.merlyn.demon.co.uk/js-valid.htm> etc.

Keep the day job.


Jul 23 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.