[Javascript] Integer and String
Hakan Magnusson
hakan at backbase.com
Mon Apr 26 09:27:27 CDT 2004
I'm a bit lost, but if focus_id is always a string when you want it to
be "false", this might work.
if(typeof(focus_id)=='string') {
focus_id = x;
}
Regards,
H
Flavio Gomes wrote:
> People, I'd like an opnion about the loop I created:
>
> I pass "form" (an actual form object) as an argument to a function that
> loops over this form's inputs and validates them:
>
>
> //=============================
> function validateForm(objForm)
> {
> error_message = "";
> focus_id = "";
>
> for (x = 0; objForm[x]; x++)
> {
> name = (objForm[x].name)?objForm[x].name:"";
> switch (name)
> {
> case "id_number":
> if(!objForm[x].value) //if value wasnt informed
> {
> error_message += "* \"ID Number\" must be informed\n";
> if(!focus_id) { focus_id = x; } //if "focus_id" isnt set, then
> store it
> }
> break;
>
> case "member_name":
> if(!objForm[x].value) //if value wasnt informed
> {
> error_message += "* \"Member Name\" must be informed\n";
> if(!focus_id) { focus_id = x; } //if "focus_id" isnt set, then
> store it
> }
> break;
> }
> }
>
> if(error_message != "")
> {
> alert(error_message);
> objForm[focus_id].focus(); //Focus the input whose focus_id was stored
> return false;
> }
> }
> //=============================
>
>
>
> Now you ask me: "Ok, and what's the problem":
>
> In the line where I commented "if(!focus_id) { focus_id = x; }" the
> "x" for "id_number" is "0" and even if I store it, on the next check
> "if(!focus_id)" it returns true and set the "focus_id" to the next input...
> The nearest to solution that I found was this:
> "if(focus_id.toString() != parseInt(focus_id));", and that's what I'd
> like to hear suggestions on.
>
> Thanks in advance,
>
> --
> Flavio Gomes
> flavio at economisa.com.br <mailto:flavio at economisa.com.br>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
More information about the Javascript
mailing list