not sure what is stopping my code from running

Discussion in 'Javascript' started by brian, Oct 24, 2006.

  1. brian

    brian Guest

    i broke down where i think the problems areas would be.
    any help would be greatly appreciated.

    where the file is called
    <script type="text/javascript" src="k.js">
    </script>


    the beginning of my form, test.cgi doesnt exsist but i think it should
    run the function before the action. Is this correct???
    <form name="courseform" action="test.cgi" method="post"
    onsubmit="return checkForm(this);">

    then my code in the file

    not sure if it would be better to do the getElementById or just
    theform.last_name.value
    I havent finished the script but i think you get the idea.

    i know the long number are a bit off the wall but i have to use them
    sadly.

    well thank you for looking over my code.
    brian
    <script>

    function checkForm(theForm)
    {
    alert("yes");
    var error = "";
    error += checkFname(theForm..getElementById('first_name').value);
    error += checkLname(theForm.last_name.value);
    error += checkPhone(theForm.phone.value);
    error += checkEmail(theForm.email.value);
    error += checkCntmethod(theForm.00N40000001U1MW.selectedIndex);
    error += checkCnttime(theForm.00N40000001U1Mm.value);
    error += checkAgency(theForm.company.value);
    error += checkAtype(theForm.00N40000001U1MR.selectedIndex);
    error += checkAaddress(theForm.street.value);
    error += checkAcity(theForm.city.value);
    error += checkAcounty(theForm.00N30000001E9CN.value);
    error += checkAzip(theForm.zip.value);
    error += checkCrsname(theForm.00N40000001U1MN.selectedIndex);
    error += checklanguage(theForm.00N40000001U2Z8.selectedIndex);
    error += checkPdate(theForm.00N40000001U1Mx.value);
    error += checkPtime(theForm.00N40000001U1NF.selectedIndex);
    error += checkAdate(theForm.00N40000001U1Li.value);
    error += checkAtime(theForm.00N40000001U1Lo.selectedIndex);
    error += isEmpty(theForm.notempty.value);
    error += isDifferent(theForm.different.value);

    if (error != "") {
    alert(error);
    return false;
    }
    return false;
    }

    //checks the first name for null statement and length
    function checkFname(fname){
    var errror = "";
    if(fname ==""){
    error = "You did not enter a first name. /n"
    }
    else if (fname.length < 2 || fname.length > 15){
    error = "The name entered is incorrect. /n"
    }
    return error;
    }

    //checks last name for null statement
    function checkLname(lname){
    var errror = "";
    if(lname ==""){
    error = "You did not enter a last name. /n"
    }
    return error;
    }

    function checkPhone(phone){
    var errror = "";
    if(phone ==""){
    error = "You did not enter a phone number. /n"
    }
    else if(phone.length == 10){
    error = "Your phone number is the incorrect length. /n"
    }
    return error;
    }

    function checkEmail(email){
    var errror = "";
    if(email ==""){
    error = "You did not enter an email address. /n"
    }
    return error;
    }

    //checks the contact method
    function checkCntmethod(choice) {
    var error = "";
    if (choice == 0) {
    error = "You did not choose a Best cotact method.\n";
    }
    return error;
    }

    function checkCnttime(conTime){
    var errror = "";
    if(conTime ==""){
    error = "You did not enter a contact time. /n"
    }
    return error;
    }

    function checkAgency(agency){
    var errror = "";
    if(agency ==""){
    error = "You did not enter your agency's name. /n"
    }
    return error;
    }

    function checkAtype(choice) {
    var error = "";
    if (choice == 0) {
    error = "You did not choose an agency type.\n";
    }
    return error;
    }

    function checkAaddress(address){
    var errror = "";
    if(adress ==""){
    error = "You did not enter the agency's address. /n"
    }
    return error;
    }

    function checkAcity(city){
    var errror = "";
    if(city ==""){
    error = "You did not enter the agency's city. /n"
    }
    return error;
    }

    function checkAcounty(county){
    var errror = "";
    if(county ==""){
    error = "You did not enter the agency's city. /n"
    }
    return error;
    }
    </script>
     
    brian, Oct 24, 2006
    #1
    1. Advertising

  2. brian

    web.dev Guest

    brian wrote:
    > <script>


    In your external javascript file, you do not need the script tag.

    > function checkForm(theForm)
    > {
    > alert("yes");
    > var error = "";
    > error += checkFname(theForm..getElementById('first_name').value);


    There's a typo. You have two '.', but even then, is the
    getElementById() method necessary?

    [snip]

    >
    > if (error != "") {
    > alert(error);
    > return false;
    > }
    > return false;
    > }


    This may be problematic. You're saying, if there's a error, alert the
    messages and do *not* submit the form. However, at the end you're also
    saying, do *not* submit the form. You probably meant to return true at
    the end, thus submitting the form to the action that you've placed.

    [snip]
     
    web.dev, Oct 24, 2006
    #2
    1. Advertising

  3. brian

    Randy Webb Guest

    web.dev said the following on 10/24/2006 6:32 PM:
    > brian wrote:
    >> <script>

    >
    > In your external javascript file, you do not need the script tag.
    >
    >> function checkForm(theForm)
    >> {
    >> alert("yes");
    >> var error = "";
    >> error += checkFname(theForm..getElementById('first_name').value);

    >
    > There's a typo. You have two '.', but even then, is the
    > getElementById() method necessary?


    It is not only un-necessary but it will throw an error as FORM elements
    do not have a method of getElementById - it belongs to the document
    alone. Not even the - window - object can use it.

    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javascript FAQ - http://jibbering.com/faq
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, Oct 24, 2006
    #3
  4. brian

    ASM Guest

    brian a écrit :
    > the beginning of my form, test.cgi doesnt exsist but i think it should
    > run the function before the action. Is this correct???


    yeap

    > <form name="courseform" action="test.cgi" method="post"
    > onsubmit="return checkForm(this);">
    >
    > then my code in the file
    >
    > not sure if it would be better to do the getElementById or just
    > theform.last_name.value


    certainly not doing :
    checkFname(theForm..getElementById('first_name').value);
    --------------------^^
    only one point please !

    Your function checkForm() is out on its first line :-(

    Anyway it is always better to address to elements of a form
    by the tree of forms :

    var elt =document.forms['myForm'].elements['myElement']
    or
    var elt =document.myForm.myElement
    or
    var f = document.forms['myForm'];
    var elt = f.myElement;

    var val = elt.value

    > <script>
    >
    > function checkForm(theForm)
    > {
    > alert("yes");
    > var error = "";
    > error += checkFname(theForm..getElementById('first_name').value);

    |
    here ------------------------------+

    > error += checkLname(theForm.last_name.value);

    (snip
    > if (error != "") {
    > alert(error);
    > return false;
    > }
    > return false;


    no ! return true; (except if it was for your tests)

    > }




    > else if(phone.length == 10){
    > error = "Your phone number is the incorrect length. /n"


    I'm sorry not in France !

    and what you do with spaces ?


    > }
    > return error;
    > }



    --
    ASM
     
    ASM, Oct 25, 2006
    #4
  5. brian

    RobG Guest

    brian wrote:
    > i broke down where i think the problems areas would be.
    > any help would be greatly appreciated.


    In addition to what others have said:

    [...]

    > function checkForm(theForm)
    > {
    > alert("yes");
    > var error = "";
    > error += checkFname(theForm..getElementById('first_name').value);
    > error += checkLname(theForm.last_name.value);
    > error += checkPhone(theForm.phone.value);


    The += compound operator is very slow in at least one popular browser,
    you may find it quicker to concatenate results:

    var error =
    checkFname(theForm..getElementById('first_name').value)
    + checkLname(theForm.last_name.value)
    + checkEmail(theForm.email.value)
    + ...


    > error += checkEmail(theForm.email.value);
    > error += checkCntmethod(theForm.00N40000001U1MW.selectedIndex);


    To be valid, id and name attributes must start with a letter.

    [...]
    > if (error != "") {
    > alert(error);


    Your users may find it more helpful if you write errors to the page,
    that way they can still see the message while they fix them. Using an
    alert, they have to remember what the errors were - and they may get 20
    of them.

    > return false;
    > }
    > return false;


    As Steph said, that should be true.

    > }
    >
    > //checks the first name for null statement and length
    > function checkFname(fname){
    > var errror = "";
    > if(fname ==""){
    > error = "You did not enter a first name. /n"
    > }


    Given that the local variable is called 'errror', this will create a
    global variable 'error' and give it a value, but only if the function
    is called. That may cause unexpected and difficult to find problems
    elsewhere.

    You have multiple instances of this error. It can be avoided by
    removing the local 'error' variable and just returning the message:

    function checkFname(fname) {
    if (fname =="") {
    return "You did not enter a first name. /n"
    } else if (fname.length < 2 || fname.length > 15){
    return "The name entered is incorrect. /n"
    }
    }

    The advice returned by the else branch doesn't seem to fit the test,
    you should say something like "First name must be from 2 to 15
    characters inclusive".


    > //checks last name for null statement


    It checks to see if the value is an empty string - null is a special
    value in javascript.

    > function checkLname(lname){
    > var errror = "";
    > if(lname ==""){
    > error = "You did not enter a last name. /n"
    > }
    > return error;
    > }
    > [...]
    > function checkAcounty(county){
    > var errror = "";
    > if(county ==""){
    > error = "You did not enter the agency's city. /n"


    Or maybe the county?


    There may be other errors...

    --
    Rob
     
    RobG, Oct 25, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Keith G
    Replies:
    0
    Views:
    1,644
    Keith G
    Sep 8, 2003
  2. Jon Delano
    Replies:
    3
    Views:
    555
    Jon Delano
    May 19, 2006
  3. Replies:
    0
    Views:
    564
  4. SenthilVel
    Replies:
    1
    Views:
    141
    Guest
    Jan 18, 2005
  5. Jack
    Replies:
    5
    Views:
    150
Loading...

Share This Page