469,631 Members | 1,147 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,631 developers. It's quick & easy.

Calculated form field numerical amount IF ELSE

MS
Hello,

I have a form with a LoanRequired field which accepts numeric data only.
If user enters >14900 then I want the user to continue as normal with the
default form.
If user enters <1000 then I want a prompt to say "You must apply for at
least £1000"
If user enters between 1000 and 149000 I want to prompt "You must complete
another form" and then redirect to another URL.

Here is my code:
<script>
function calcLoanRequired()
{
if (document.form1.LoanRequired.value > 149000)
{
}
else if (document.form1.LoanRequired.value < 1000)
{
}
else
{
alert("GOING TO OTHER FORM")
document.location.href=("http://lowvalue.co.uk")
}
}
</script>

Now, I get 100 enquiries a day, and although 90 of them go to the right
place, it seems around 10 a day are ignoring the script and the user is able
to submit the form with a value of between 1000 and 149000. The function is
called onBLur (the LoanRequired form field), onClick (The submit button) and
onSubmit(The form tag).

Any ideas?

Thanks!!!

Gary.
PS - I CANNOT REPLICATE THE PROBLEM, I HAVE TRIED EVERYTHING!!!!!

Jul 20 '05 #1
8 1623
On Thu, 22 Jan 2004 19:12:48 GMT, MS <ms@garywhittle.co.uk> wrote:

[snip]
<script>
The type attribute is required for SCRIPT elements. That tag should read:

<script type="text/javascript">

[snip]
Now, I get 100 enquiries a day, and although 90 of them go to the right
place, it seems around 10 a day are ignoring the script and the user is
able to submit the form with a value of between 1000 and 149000. The
function is called onBLur (the LoanRequired form field), onClick (The
submit button) and onSubmit(The form tag).

Any ideas?


Not all users have JavaScript enabled and so your code will have no effect
whatsoever.

ALWAYS validate server-side.

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #2
MS
ALWAYS validate server-side.

Mike

Thanks,

Any pointers on getting started with this? Never tried before.

Gary.
Jul 20 '05 #3
MS
> Not all users have JavaScript enabled and so your code will have no effect
whatsoever.

Hi Mike,

I also collect information about the browser, and whether or not the user
has JavaScript enabled - and even when these things are true, the user still
manages to sometimes get past the script.

Gary.
Jul 20 '05 #4
In article <AP*********************@news-text.cableinet.net>,
ms@garywhittle.co.uk enlightened us with...
ALWAYS validate server-side.

Mike

Thanks,

Any pointers on getting started with this? Never tried before.

Gary.


Where is the form submitting to?
ASP? JSP? Perl? PHP?
--
--
~kaeli~
User: The word computer professionals use when they mean
'idiot'.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 20 '05 #5
On Thu, 22 Jan 2004 21:00:58 GMT, MS <ms@garywhittle.co.uk> wrote:
Not all users have JavaScript enabled and so your code will have no
effect whatsoever.


Hi Mike,

I also collect information about the browser, and whether or not the user
has JavaScript enabled - and even when these things are true, the user
still manages to sometimes get past the script.


The only other possibility that I can think of at the moment is that your
comparisons are at fault. You are comparing a string to a number, and if
the proper type conversion doesn't occur, the comparison might fail.

Try converting the strings to numbers, then compare them to be sure.

function calcLoanRequired() {
// give this a more appropriate name
var number = Number( document.form1.LoanRequired.value );

if ( isNaN( number )) {
// Value entered wasn't actually a number
// Cancel the submission and alert the user
} else {
if ( number > 149000 ) {
} else if ( number < 1000 ) {
} else {
alert("GOING TO OTHER FORM");
document.location.href = "http://lowvalue.co.uk";
}
}
}

It's a shame you can't see a pattern in the erroneous submissions.

Good luck,
Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #6
MS
> Try converting the strings to numbers, then compare them to be sure.

function calcLoanRequired() {
// give this a more appropriate name
var number = Number( document.form1.LoanRequired.value );

if ( isNaN( number )) {
// Value entered wasn't actually a number
// Cancel the submission and alert the user
} else {
if ( number > 149000 ) {
} else if ( number < 1000 ) {
} else {
alert("GOING TO OTHER FORM");
document.location.href = "http://lowvalue.co.uk";
}
}
}


Hi Mike,

Thanks for the time your spending trying to help me. I have cracked it, its
not the numbers as I have "number only" script on the form field.

Here is what is happening.
When a user hits the submit button and the rate is lower than £149,000 it is
supposed to go to another file. However, it first dsiplays an alert on the
page explaining that a new page is going to be loaded. The onClick
behaviour, has a second function applied to it, which submits the form.
Because of the alert, IE does not actually "appear" to submit the form, even
though it has in fact sent the data. Then, when you press OK on the alert,
the redirection occurs.

Sorted now! :)

Thanks guys,

Gary.
Jul 20 '05 #7
On Thu, 22 Jan 2004 19:12:48 GMT, "MS" <ms@garywhittle.co.uk> wrote:
Hello,

I have a form with a LoanRequired field which accepts numeric data only.
If user enters >14900 then I want the user to continue as normal with the
default form.
If user enters <1000 then I want a prompt to say "You must apply for at
least £1000"
If user enters between 1000 and 149000 I want to prompt "You must complete
another form" and then redirect to another URL.

Here is my code:
<script>
function calcLoanRequired()
{
if (document.form1.LoanRequired.value > 149000)
{
}
else if (document.form1.LoanRequired.value < 1000)
{
}
else
{
alert("GOING TO OTHER FORM")
document.location.href=("http://lowvalue.co.uk")
}
}
</script>

Now, I get 100 enquiries a day, and although 90 of them go to the right
place, it seems around 10 a day are ignoring the script and the user is able
to submit the form with a value of between 1000 and 149000. The function is
called onBLur (the LoanRequired form field), onClick (The submit button) and
onSubmit(The form tag).

Any ideas?

Thanks!!!

Gary.
PS - I CANNOT REPLICATE THE PROBLEM, I HAVE TRIED EVERYTHING!!!!!

Maybe the end users dont have Javascript enabled...

HTH
Al.
Jul 20 '05 #8
JRS: In article <Qg*********************@news-text.cableinet.net>, seen
in news:comp.lang.javascript, MS <ms@garywhittle.co.uk> posted at Thu,
22 Jan 2004 19:12:48 :-
function calcLoanRequired()
{
if (document.form1.LoanRequired.value > 149000)
{
}
else if (document.form1.LoanRequired.value < 1000)
{
}
else
{
alert("GOING TO OTHER FORM")
document.location.href=("http://lowvalue.co.uk")
}
}


Minor point : for efficiency, use

var V = document.form1.LoanRequired.value

and then refer just to V.

You have possible problems other than those in the answers I have seen.

What happens if the string contains £12345 or 12,345 or £12,345 ?
Or even €100000 ? (that's a euro sign). It will, converted to Number,
be NaN, therefore unequal to anything.

You should validate the input with a RegExp to ensure that it contains
only digits, after which you can make V into a number by

var V = + document.form1.LoanRequired.value

which increases efficiency too.

Read the FAQ.

Read <URL:http://www.merlyn.demon.co.uk/js-valid.htm>.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for 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 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.