I am very new to JavaScript and am having trouble getting my form to validate the entries the way I want. I am trying to get the form to require an email address or street address. Both are OK but neither is not OK. I have been able to get it to work if I require everything. Here is what I was able to come up with. Any help would be greatly appreciated.

Thank you so much for the help. I am very new to JavaScript so the suggestions/notes are very helpful. I am confused about two things.
1. what do the var emre and var trim mean?
2. does the new code go on a standalone JS page or is it inserted into the HTML?

Thank you for the help and sorry if these questions are stupid. The book I am using to learn this seems to be a little dated. Do you have any recommendations of books/sites for learning JavaScript? I have looked on google, etc but I'm not sure which are good and which aren't.

Thank you,
David

Logic Ali

10-02-2012, 03:15 AM

if ( se.length > 7 ) return true;

The intermediate return statement strikes again.

You can't return true if there are more tests to perform.

Old Pedant

10-02-2012, 03:18 AM

(1) The variables emre and trim are variables that are holding REGULAR EXPRESSIONS. You can look up what that means, but for now just use them and be happy.

(2) You can put the JS either in the HTML page or in a separate JS file. Makes no difference.

(You don't have to get rid of the name of the <form>, but it serves no purpose once you pass the reference to the <form> into the function using the this keyword.)

There are some "sticky" threads at the head of this JavaScript forum with references to sites to look at.

*************

As an aside: There really is no reason to give your <form> fields ids as well as names. No place in your code do you even use or need the ids. Note that a form field WITHOUT a name will *NOT* be sent on to the next page as part of the form submittal. So name are needed for the form submittal; id's are not, at all.

Having just said the above, there are a few circumstances where form field id's can come in handy, but you haven't encountered them yet and likely won't for a while.

Old Pedant

10-02-2012, 03:20 AM

The intermediate return statement strikes again.

You can't return true if there are more tests to perform.

WRONG.

Read his requirements. He WANTS to accept EITHER a street address OR an email address as sufficient.

So my code is simply saying "if you get a street address, there is no need to then check the email address."

Would this trick work if there were *OTHER* validations to perform, AFTER the street address and email checks? Of course not. But that's exactly why I placed those two checks last.

Logic Ali

10-02-2012, 04:10 AM

WRONG.

Read his requirements. He WANTS to accept EITHER a street address OR an email address as sufficient.

So my code is simply saying "if you get a street address, there is no need to then check the email address."

In that case the solution should make the intention unambiguously clear, and splattering return statements everywhere isn't going to do that.

If you want a *good* solution, then you shouldn't be using alert( ) ... which I already mentioned. This is clearly a thread about a newbie first doing validation. I gave a working solution for this simple case. If you want really GOOD validation, it shouldn't be done this way in any case.

Your validation is horribly faulty, to me, in that you will only show the first error message and not all validation errors. In that, you share the coding with the original. But that still doesn't make it good validation.

Finally, if you are (as you seem to be) one of those purists who believe that functions should never return early, then I have nothing more to say. We will never agree.