Click to See Complete Forum and Search --> : Can i include a function in an IF statement.
Hi,
I am tryin to check for a field in javascript.Since this field is not a required field in the form,If there is no entry in the field,we go to the next edit ,if there is a value in the field then we need to make sure that value conforms a certain format.I check for this format usin the validate function.But how do i check if i want to do go into this loop,if there is no value entered in this field,i shud just skip it all.I tried this,but it does'nt work.
if( document.forms[0].sid.value != "" )
{
function validate ( sid )
{
return /^MO\d{8}$/.test(document.forms[0].sid.value);
}
if (!validate( document.forms[0].sid.value ) )
alert("Please make sure SID number starts with 'US' followed by 8 numbers");
}
Thanks in advance..
Phil Karras
07-16-2003, 11:56 AM
Try something like:
if(document.forms[0].sid.value) { // If a value check it
validate (document.forms[0].sid.value);
return /^MO\d{8}$/.test(document.forms[0].sid.value);
}
// otherwise we skip the check
Or you could check to see if the value has a length:
if(document.forms[0].sid.value.length) { // If a value check it
which means something was entered.
The Validate function should NOT be in the function, it should be in the <head> <script...> section of the HTML file or in a JS file included in the <HEAD> <Script ...> section.
I am using a JScript file in a larger program in notes.I dont really want to include the validate function in the header cuz this javascript is huge and for simplicity sakes i wud rather have it in the program rite there.This validate function works fine when i am just checking for the field bieng in a certain format.JUst recently while testing we realized that if there is no value in the field,we dont need to check for the format,while initially my code did check even if there was a blanck.Now i need to make sure i run this validate and checking for the format only if there is something there in the field.how can i check for this.I do need the alert box as used in my code when i am checking for format in the field.
please help..
Thanks
Phil Karras
07-16-2003, 03:12 PM
...checking for the format only if there is something there in the field.how can i check for this?
See my above posting, checking for the length should do this.
This is what i am doing but it still does'nt work.Even when the field is empty,it throws up the alert box as in the following code.How do imake it not throw the alert box when there is no value entered in the sid field.
if(document.forms[0].sid.value)
{ // If a value check it
validate (document.forms[0].sid.value);
return /^US\d{8}$/.test(document.forms[0].sid.value);
if (!validate( document.forms[0].sid.value ) )
alert("Please make sure SID number starts with 'US' followed by 8 numbers");
}
Phil Karras
07-16-2003, 04:06 PM
You still haven't done what I suggested.
I thot i did just that!!!
Even while checking for length,it does'nt work.it still throws up the alert box...which i dont want it to???
what r u exactly suggesting,cuz i thot i ve incorporated ur logic in my code..
Thanks,,
Phil Karras
07-17-2003, 07:21 AM
OK, now at least you told me that you did check my suggestion.
So, here's another: Since you can't get it to work it is because you (and I at this point)
have no idea what in fact document.forms[0].sid.value is returning when it
has "nothing", so, just before you check its value, put an alert statement to see
what it really is.
alert(document.forms[0].sid.value); // What am I, really?
if(document.forms[0].sid.value) {
.
.
.
}
I still cant get it to work..The alert box that u suggested does'nt throw up..
DO u have any other ideas..
Anybody..
Phil Karras
07-17-2003, 01:42 PM
Originally posted by Ann
I still cant get it to work..The alert box that u suggested does'nt throw up..
Doesn't "throw up" what? do you mean it doesn't pop up?
First things first, if it is not popping up you better find out why
since that is really basic coding there.
Once you get it woroking you should be able to find out what
the code is seeing as values which whill help you find a way to
get it to do what you want.
If you really want my time I am a consultant and I am willing
to help for my normal consulting fee (actually do the coding
for you), contact me directly if so.
Thanks for ur suggestions.
I dont think,I quite came to this forum to pay anybody to do my work.I just wanted to know where i was going wrong.I would'nt have the money to afford your services anyways :)
I'll look someplace else.
Phil Karras
07-18-2003, 02:14 PM
You are welcome & you do not have to pay me or anyone else. I
simply suggested it because I need the work & you seem to be
really stuck. If this is a company project perhaps they would be
willing to pay for a few hours to get you over the problem.
If this is personal, just keep asking. I'm still willing to help
for free. The problem, as I see it, is that we are not
communicating. I do not see why you're having problems so you
either are not explaining the problem completely, or you're
missing some basic bit that once understood will make it all
clear.
So once again, have you gotten the alert statement working yet?
If not, do you have access to Netscape 6.1 or newer? The debugger
on NS is usually much better than that on IE & it may tell you
things that IE doesn't or can't.
Also, can you make a KISS example of the problem for me to play
with?
KISS = Keep It Short & Simple
The most common problem that causes an alert statement to
NOT work is that there is a bug somewhere before the alert.