473,387 Members | 1,497 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,387 software developers and data experts.

IF Statement Problems (or possibly not!)

I'm writing an if statement for a UK credit card form validation
script. Users who specify that their card is Switch need to enter
either the issue number or the 'valid from' date.

I'm trying to write an if statement so that if:

1. The word "Month" is in the validFromMonth field (where they haven't
selected a month a the drop-down menu) or the word "Year" is in the
validFromYear field (where they haven't selected a year another the
drop-down menu)
OR
2. Nothing has been entered in the text box for the Issue_Number

then the alert box appears

<SCRIPT language="text/javascript">
function checkform()
{
if (document.Final.Cardtype.value == "")
{
alert("Please specify the type of debit or credit card.");
return false;
}
if (document.Final.Cardholder.value == "")
{
alert("Please enter the Cardholder's Name.");
return false;
}
if (document.Final.Cardnumber.value == "")
{
alert("Please enter the card number.");
return false;
}
//check whether they need to supply an issue number/start date
var cc_type = document.Final.Cardtype.options[document.Final.Cardtype.selectedIndex].value;
if (cc_type == "Switch")
{
// if card is Switch then we need an issue number or start date

var IssNum = document.Final.Issue_Number.value
var fromM = document.Final.validFromMonth.value
var fromY = document.Final.validFromYear.value

if ((IssNum == "") || ((fromM == "Month") || (fromY == "Year")))
{
alert("You must supply an issue number or a 'Valid From' date for
Switch");
return false;
}
}
// if we reach this point then we submit the form.
return true;
}
//-->
</script>

By enclosing (fromM == "Month") || (fromY == "Year") in their own
brackets, I was hoping it would evaluate that first but it doesn't
seem to be working. The form wants a correct entry in all three before
allowing progress.

[I know I haven't written the checking bit for the expiry date yet,
I'm doing it in order!]

Any ideas?
Jul 23 '05 #1
3 1710
> if ((IssNum == "") || ((fromM == "Month") || (fromY == "Year")))
{
alert("You must supply an issue number or a 'Valid From' date for
Switch");
return false;


The problem is that is IssNum != "" it still goes and validates the
other. This will never pass because unless you fill in ALL the blanks
it would never pass. the complecated thing is that you have to check
for all invalid combinations.

// issue number not filled in, and no month or year.
if ((IssNum == "") && ((fromM=="Month") || (fromY=="Year")))
{
alert("You must supply an issue number or....");
}

Though this will pass is someone enters an issue number and a valid
date. If you don't want that you should add that exception with an
or.

Good luck,
Vincent

Jul 23 '05 #2
What's your point? I mean, what the problem is?

ccton
--
www.vicdir.com
"Mark Morton" <ma**@bustingmychops.com> ????
news:e4**************************@posting.google.c om...
I'm writing an if statement for a UK credit card form validation
script. Users who specify that their card is Switch need to enter
either the issue number or the 'valid from' date.

I'm trying to write an if statement so that if:

1. The word "Month" is in the validFromMonth field (where they haven't
selected a month a the drop-down menu) or the word "Year" is in the
validFromYear field (where they haven't selected a year another the
drop-down menu)
OR
2. Nothing has been entered in the text box for the Issue_Number

then the alert box appears

<SCRIPT language="text/javascript">
function checkform()
{
if (document.Final.Cardtype.value == "")
{
alert("Please specify the type of debit or credit card.");
return false;
}
if (document.Final.Cardholder.value == "")
{
alert("Please enter the Cardholder's Name.");
return false;
}
if (document.Final.Cardnumber.value == "")
{
alert("Please enter the card number.");
return false;
}
//check whether they need to supply an issue number/start date
var cc_type = document.Final.Cardtype.options[document.Final.Cardtype.selectedIndex].value
; if (cc_type == "Switch")
{
// if card is Switch then we need an issue number or start date

var IssNum = document.Final.Issue_Number.value
var fromM = document.Final.validFromMonth.value
var fromY = document.Final.validFromYear.value

if ((IssNum == "") || ((fromM == "Month") || (fromY == "Year")))
{
alert("You must supply an issue number or a 'Valid From' date for
Switch");
return false;
}
}
// if we reach this point then we submit the form.
return true;
}
//-->
</script>

By enclosing (fromM == "Month") || (fromY == "Year") in their own
brackets, I was hoping it would evaluate that first but it doesn't
seem to be working. The form wants a correct entry in all three before
allowing progress.

[I know I haven't written the checking bit for the expiry date yet,
I'm doing it in order!]

Any ideas?

Jul 23 '05 #3
Mark Morton wrote:
I'm writing an if statement for a UK credit card form validation
script. Users who specify that their card is Switch need to enter
either the issue number or the 'valid from' date.
Whatever validation you do on the client _must_ be duplicated on the server. The client-side
validation is for the user's benefit only, there are any number of ways of submitting the form
without completing the form as specified in your client-side validation.
I'm trying to write an if statement so that if:

1. The word "Month" is in the validFromMonth field (where they haven't
selected a month a the drop-down menu) or the word "Year" is in the
validFromYear field (where they haven't selected a year another the
drop-down menu)
OR
2. Nothing has been entered in the text box for the Issue_Number

then the alert box appears

<SCRIPT language="text/javascript">
<script type="text/javascript">

The LANGUAGE attribute is deprecated, and used incorrectly above.
function checkform()
Presumably you're using <form ... onsubmit="return checkform();">? Change it to <form ...
onsubmit="return checkform(this);"> and the function definition above to be:

function checkform(myForm)
{
if (document.Final.Cardtype.value == "")
I'm assuming Cardtype is a <select>? If so, use

if (myForm.elements['Cardtype'].options[myForm.elements['Cardtype'].selectedIndex].value == "")

Or if the first <option> is the empty one, simply:

if (myForm.elements['Cardtype'].selectedIndex == 0)

if (document.Final.Cardholder.value == "")
if (myForm.elements['Cardholder'].value == "")

Note that if the user enters one or more spaces, this test will think the input has data in it. To
fix this, use the string manipulation at:

<url: http://jibbering.com/faq/#FAQ4_16 />

Then you can use:

if (myForm.elements['Cardholder'].value.trim() == "")
if (document.Final.Cardnumber.value == "")
Same as above.
//check whether they need to supply an issue number/start date
var cc_type = document.Final.Cardtype.options[document.Final.Cardtype.selectedIndex].value;
You're retrieving the value of a <select> correctly here, yet above and below this you seem to be
doing it by simply retrieving the .value attribute of the <select>. Also, you should do this
earlier, then you can use cc_type to test whether they've selected a CC type at all.
if (cc_type == "Switch")
{
// if card is Switch then we need an issue number or start date

var IssNum = document.Final.Issue_Number.value
var fromM = document.Final.validFromMonth.value
var fromY = document.Final.validFromYear.value
Retrieve these <select> values using the syntax shown earlier.
if ((IssNum == "") || ((fromM == "Month") || (fromY == "Year")))
From your surrounding text and output of the alert(), I'm assuming you need either an issue number
or valid "From" date. So your logic is (in english): show an error when IssNum is empty, or fromM
is "Month" and fromY is "Year" (because that means a complete date hasn't been chosen). Converting
this to Javascript gets you:

if (IssNum == "" || (fromM == "Month" && fromY == "Year"))

The brackets are not actually required since && has higher precedence then ||, but I like to make
the intent clear.
//-->
Not needed. Omit.
</script>

By enclosing (fromM == "Month") || (fromY == "Year") in their own
brackets, I was hoping it would evaluate that first but it doesn't
seem to be working. The form wants a correct entry in all three before
allowing progress.
Javascript evaluates conditions from left to right (observing precedence rules and brackets on the
way). Once it's determined conclusively the truth or falseness of the entire condition, it stops
evaluating. This means if you do something like:

if (false && whateverElseYouWantToTest())

the function "whateverElseYouWantToTest()" will _never_ be executed. Javascript sees the result of
the part before the && is false. It's an &&, both expressions _must_ be true for the entire
condition to be true, but the first expression is false. It does not matter what the second
expression returns, the entire condition will always be false, so Javascript stops after seeing
"false".

This is what allows you to do:

if (someObject != null && someObject.someMethod())

normally, trying to call someMethod() on a null someObject would cause an error, but when
someObject == null, the first expression of the && is false, therefore the entire condition is
false and Javascript never even executes the 2nd expression. This lets you do things like:

With a set of || conditions, Javascript must always evaluate all 3 expressions to determine if any
of them are true.

But in your case the logic was simply wrong.
Any ideas?


Doing it the way you are doing it is annoying, because you tell the person one thing they did
wrong, then they correct that and try to submit again only to be told the next thing they did
wrong, so they correct that, then they're told the 3rd thing they did wrong, and so on.

Better to build a string containing all the mistakes they made and alert everything at once.

Something like:

var errorMsg = [];
var firstFieldToFocus;
if (formField1 != 'whatever') {
errorMsg.push('Test1 failed');
if (!firstFieldToFocus) {
firstFieldToFocus = formField1;
}
}
if (formField2 != 'whatever') {
errorMsg.push('Test2 failed');
if (!firstFieldToFocus) {
firstFieldToFocus = formField2;
}
}
// etc

if (errorMsg.length > 0) {
alert(
'Please correct the following' +
' errors before continuing:\n\n- ' +
errorMsg.join('\n- ')
);
if (firstFieldToFocus) {
firstFieldToFocus.focus();
}
}

The code also places the cursor in the first of the input boxes containing errors. This behaviour
is optional, your users may not appreciate the form scrolling almost to the top, they may prefer
to fix the problems in their own order.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #4

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

Similar topics

1
by: Maria | last post by:
Hello we got a script automatically generated to populate a table that belongs to a partition table, then while executing we get this error message: "DB21034E The command was processed as an...
2
by: Jon | last post by:
Hi all I know this is not the preferred newsgroup, but you people are great with SQL MS ACCESS, VISUAL BASIC6 Hope you can see past my table, string definitions (Swedish), just ask otherwise ...
5
by: hamishd | last post by:
Hello, In my application I create some large vectors to hold data. For example: std::vector<DataItemClass*MyData; DataItemClass * DataItem; for(i=0;i<SomeLargeLimit;i++){ DataItem = new...
1
by: Billy | last post by:
I do a SELECT * from table command in an ASP page to build a text file out on our server, but the export is not to allow a field name rows of records. The first thing I get is a row with all the...
0
by: PeterA | last post by:
I have a Windows PC connecting to ISeries with ODBC. The windows PC uses various SQL statements to connect to DB2 and download/upload data. Data downloads/uploads appear to be OK and I can't see...
1
by: patriciashoe | last post by:
Everything I read indicates the following statement should work. What happens is that any field after the where clause gets treated as a parameter by Access. Even if I supply the data the query...
2
by: Lebbsy | last post by:
I have a datasheet form that on opening I want to select only the records whose Decision field value is "Recommend". I have the following SQL statement so far but it gives me the "cannot execute...
1
Fary4u
by: Fary4u | last post by:
Hi, I'm attempting to update a record via the recordset.update method, However, the recordset object is defined and accesses the correct record that I want to update, as I can retrieve field...
5
by: Mr Hero | last post by:
I need assitance with my IIF statement. IIf(!!="1",True,IIf(!!="2",False,IIf(!!="3",,))) I have a column in my table that stores values from a check box in my form. I have a form that has...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.