New guy needs help

G

greg.gottfried

I am very new to java script. I am wanting to validate a form and I
cant seem to get one validation to work. The first two work, but the
thrid I want to have a max length of 30 and I cant ssem to figure out
how to do that. Please help!

var msg = "";
if(document.jobOpening.jobCity.value == ""){
msg = msg + "Please provide a Job City before submitting the form.";
alert(msg);
document.jobOpening.jobCity.focus();
return false;
}
if(document.jobOpening.jobCounty.value == ""){
msg = msg + "Please provide a Job County before submitting the
form.";
alert(msg);
document.jobOpening.jobCounty.focus();
return false;
}
if(document.jobOpening.jobName.value =="", "maxlen=30"){
msg = msg + "Please provide a Job Name Less than 30 Char. before
submitting the form.";
alert(msg);
document.jobOpening.jobName.focus();
return false;
}
 
R

Randy Webb

(e-mail address removed) said the following on 4/19/2006 4:28 PM:
I am very new to java script. I am wanting to validate a form and I
cant seem to get one validation to work. The first two work, but the
thrid I want to have a max length of 30 and I cant ssem to figure out
how to do that. Please help!

if(document.jobOpening.jobName.length>29)
 
G

greg.gottfried

I fooled around with it a bit more and came up with this.


function validateMe(){
var msg = "";
if(document.jobOpening.jobCity.value == ""){
msg = msg + "Please provide a Job City before submitting the form.";
alert(msg);
document.jobOpening.jobCity.focus();
return false;
}
if(document.jobOpening.jobCounty.value == ""){
msg = msg + "Please provide a Job County before submitting the
form.";
alert(msg);
document.jobOpening.jobCounty.focus();
return false;
}
if(document.jobOpening.jobName.value== ""){
msg = msg + "Please provide a Job Name before submitting the form.";
alert(msg);
document.jobOpening.jobName.focus();
return false;
}
if(document.jobOpening.jobName.value.length > 31){
msg = msg + "Please provide a Job Name less than 30 Char. before
submitting the form.";
alert(msg);
document.jobOpening.jobName.focus();
return false;
}
 
R

RobG

(e-mail address removed) said on 20/04/2006 7:09 AM AEST:
I fooled around with it a bit more and came up with this.

Coding by trial and error is usually not the best methodology ;-)

[...]

Your original error was here:

if(document.jobOpening.jobName.value =="", "maxlen=30"){
------------------------------------------------^^^^^^^^^^^^^^^^

That is an invalid if statement.

if(document.jobOpening.jobName.value.length > 31){
msg = msg + "Please provide a Job Name less than 30 Char. before
submitting the form.";

The message does not fit the test. The test allows up to and including
30 characters, the statement says less than 30 characters - which is it?
 
G

greg.gottfried

Your right I did not have the right Char. length I want it to have up
to but not exceeding 30 char. I did fix the problem thanks to all for
your insight
RobG said:
(e-mail address removed) said on 20/04/2006 7:09 AM AEST:
I fooled around with it a bit more and came up with this.

Coding by trial and error is usually not the best methodology ;-)

[...]

Your original error was here:

if(document.jobOpening.jobName.value =="", "maxlen=30"){
------------------------------------------------^^^^^^^^^^^^^^^^

That is an invalid if statement.

if(document.jobOpening.jobName.value.length > 31){
msg = msg + "Please provide a Job Name less than 30 Char. before
submitting the form.";

The message does not fit the test. The test allows up to and including
30 characters, the statement says less than 30 characters - which is it?
 
R

Richard Cornford

RobG said:
(e-mail address removed) said on 20/04/2006 7:09 AM AEST:
Your original error was here:

if(document.jobOpening.jobName.value =="", "maxlen=30"){
----------------------------------------------^^^^^^^^^^^^^^^^

(As a point in passing, indicating sections in code as above is common
on Usenet but suffers from assumptions about the font in use to display
the message. If the viewer's character widths do not correspond with
writer's the indicator will not line up with the part of the statement
that is being pointed to. The usual convention is to write such
indicators in a fixed-width font as then the viewer is in a position to
re-produce the desired effect by switching to a fixed width font
themselves. Otherwise, it is largely a coincidence when the writer's
font corresponds sufficiently closely with the viewer's.)
That is an invalid if statement.
<snip>

Actually it is not an invalid statement. Javascript has a 'list'
operator (see ECMA 262, 3re Ed. Section 11.14), which forms an
expression out of a comma separated sequence of AssignmentExpressions,
the whole Expression evaluating as the value of the last item in this
list (though each item in the list is evaluated during the evaluation of
the whole expression). This leaves the above syntactically correct (so
as 'valid' as javascript gets), but always evaluating to the value of
the final string "maxlen=30", which type-converts to boolean true for
the evaluation of the - if - expression.

Richard.
 
R

RobG

Richard Cornford said on 20/04/2006 9:42 AM AEST:
(As a point in passing, indicating sections in code as above is common
on Usenet but suffers from assumptions about the font in use to display
the message. If the viewer's character widths do not correspond with
writer's the indicator will not line up with the part of the statement
that is being pointed to. The usual convention is to write such
indicators in a fixed-width font as then the viewer is in a position to
re-produce the desired effect by switching to a fixed width font
themselves. Otherwise, it is largely a coincidence when the writer's
font corresponds sufficiently closely with the viewer's.)

I copy/pasted a tab character that was in the original post for
indenting. On posting, that was converted to spaces so the highlight no
longer aligned with text. Not an excuse, just an explanation.

<snip>

Actually it is not an invalid statement. Javascript has a 'list'
operator (see ECMA 262, 3re Ed. Section 11.14), which forms an
expression out of a comma separated sequence of AssignmentExpressions,
the whole Expression evaluating as the value of the last item in this
list (though each item in the list is evaluated during the evaluation of
the whole expression). This leaves the above syntactically correct (so
as 'valid' as javascript gets), but always evaluating to the value of
the final string "maxlen=30", which type-converts to boolean true for
the evaluation of the - if - expression.

Thanks - now I have another obfuscation tool up my sleeve :-o.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top