Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / May 2006



Tip: Looking for answers? Try searching our database.

Beginner form validation

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jessica - 31 May 2006 21:59 GMT
Can someone look at this code and tell me what is wrong? The error
message says "Line 8 Char 1 'dealerId' is undefined" But I thought that
I stated everything correctly. Do I have any syntax errors in here. The
code is supposed to display an alert if someone typed in letters
instead of numbers for each input box.
---------------------------------------------------------------------------------------------------------------------------------------

<script>
function validateInputs() {
var validInputs=(IsValidNumber(dealerId,DealerID)
                &&IsValidNumber(seriesId,SeriesID)
                &&IsValidNumber(modelId,ModelID)
                &&IsValidNumber(extColorId,ExteriorColorID)
                &&IsValidNumber(fabricTypeId,FabricTypeID));
    if(!validInputs){
        return false;
    }
}
function IsValidNumber() {
var input = document.getElementById(inputId);
    if(!validateNumber(input,inputDescription)){
        return false;
    }else{
        return true;
    }
}

var re = /^\d+$/;
function validateNumber(input, inputName) {
    var validNumber = re.exec(input.value);

    if (!validNumber) {
        alert("Input a valid number in " + inputName + " now!");
        input.focus();
        return false
    } else {
        return true;
    }
}
</script>

html:
<form id="Form1" action="DisplayMultipleProcedures.aspx" method="post"
onsubmit="return validateInputs();">
---------------------------------------------------------------------------------------------------------------------------------------

Thanks :)
David Dorward - 31 May 2006 22:07 GMT
> Can someone look at this code and tell me what is wrong? The error
> message says "Line 8 Char 1 'dealerId' is undefined" But I thought that
> I stated everything correctly.

> <script>
> function validateInputs() {
> var validInputs=(IsValidNumber(dealerId,DealerID)

Nothing in your code defines what "dealerId" is. You didn't state it at all.

(Hint: Giving a form element a name in HTML does not create a variable in
the global JavaScript namespace with the same name that is a referenc etot
hat element. http://www.mozilla.org/docs/web-developer/upgrade_2.html#dom )

Signature

David Dorward       <http://blog.dorward.me.uk/>   <http://dorward.me.uk/>
                    Home is where the ~/.bashrc is

Dag Sunde - 31 May 2006 22:13 GMT
> Can someone look at this code and tell me what is wrong? The error
> message says "Line 8 Char 1 'dealerId' is undefined" But I thought
[quoted text clipped - 42 lines]
>
> Thanks :)

You have forgot to declare the parameters in "function IsValidNumber()":
   function IsValidNumber(inputId, inputDescription) { ...

Also, the way you use those params, you have to give them as strings
when you call "IsValidNumber(...)", like this:

function validateInputs() {
    var validInputs=(IsValidNumber('dealerId', 'DealerID')
        && IsValidNumber('seriesId', 'SeriesID')
        && IsValidNumber('modelId', 'ModelID')
        && IsValidNumber('extColorId', 'ExteriorColorID')
        && IsValidNumber('fabricTypeId', 'FabricTypeID'));

   return validInputs;
}

Signature

Dag.

Richard Cornford - 31 May 2006 22:18 GMT
> Can someone look at this code and tell me what is wrong?

Your post does not include the HTML with which this code is attempting
to interact so suggesting how it might be made to function will be
impractical.

> The error message says "Line 8 Char 1 'dealerId' is
> undefined"

'dealerId' is not defined. Indeed, there appear to be a score of
Identifiers used in this script that have never been declared, or
assigned values. The error given just marks the first runtime error
caused by accessing an undeclared variable.

> But I thought that I stated everything correctly.

What do you mean by that?

> Do I have any syntax errors in here.

Syntax errors would be reported before any code was executed, so the
fact that you are getting a runtime error indicates no syntax errors.

> The code is supposed to display an alert if someone typed in
> letters instead of numbers for each input box.
<snip>

See:-

<URL: http://www.jibbering.com/faq/faq_notes/form_access.html >

Richard.
Andy Baxter - 31 May 2006 22:20 GMT
Jessica said:

> Can someone look at this code and tell me what is wrong? The error
> message says "Line 8 Char 1 'dealerId' is undefined" But I thought that
[quoted text clipped - 6 lines]
> function validateInputs() {
> var validInputs=(IsValidNumber(dealerId,DealerID)
                                         ^^^^^^^^
"DealerID" needs to be in inverted commas - it's a string not a variable.

>                 &&IsValidNumber(seriesId,SeriesID)
>                 &&IsValidNumber(modelId,ModelID)
>                 &&IsValidNumber(extColorId,ExteriorColorID)
>                 &&IsValidNumber(fabricTypeId,FabricTypeID));
>     if(!validInputs){
>         return false;
               ^^^^^^^^^^^^^
          you don't need this - you can just do 'return validInputs'
>     }
>     }
> function IsValidNumber() {
                       ^^
You haven't defined any parameters for this function.

> var input = document.getElementById(inputId);
>     if(!validateNumber(input,inputDescription)){
>         return false;
>     }else{
>         return true;
>     }

you don't need this function - just do validateNumber(dealerId,"DealerID")

>     }
>     }
[quoted text clipped - 10 lines]
>     }
>     }

This bit looks OK.

> </script>
>
[quoted text clipped - 4 lines]
>
> Thanks :)

Signature

http://www.niftybits.ukfsn.org/

remove 'n-u-l-l' to email me. html mail or attachments will go in the spam
bin unless notified with [html] or [attachment] in the subject line.

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.