SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast BigKingy's Avatar
    Join Date
    Jun 2004
    Location
    London
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question JS Validation, how can you parse a 2 word fieldname?

    I've noticed most people use 'LastName' instead of 'Last Name' for the text box name.

    Why is this? Is there not a space equvilient in JS like %20 or nbsp; ?

    Is there anyway I could have the field name as 'Last Name' and it validate?

    Here is my current script:

    <script type="text/javascript" language="JavaScript">
    <!--
    function formValidation(form){
    if(notEmpty(form.Last Name)){
    if(notEmpty(form.Fax)){
    window.open( "http://www.yahoo.com", "thankyouPopup",
    "status = 0, toolbar = 0, location = 0, menubar = 0, directories = 0, height = 300, width = 300, resizable = 0" );
    document.form.Send.disabled=true;document.form.Send.value='Sending details (please wait)';
    return true;
    }
    }
    return false;
    }

    function notEmpty(elem){
    var str = elem.value;
    if(str.length == 0){
    alert("You must enter information in the boxes with BLUE titles to submit the form");
    return false;
    } else {
    return true;
    }

    }

    //-->
    </script>

    In its current state, it throws an error at the space after "Last" in "Last Name".

    Any ideas how i could get around this?

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The value for the name (or id) attribute may not contain whitespace in (X)HTML.
    Neither can JavaScript object properties, for that matter.
    Birnam wood is come to Dunsinane

  3. #3
    SitePoint Enthusiast BigKingy's Avatar
    Join Date
    Jun 2004
    Location
    London
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have managed a way around it, and it works fine in Internet Explorer, but it does work at all in Firefox.

    I have used the windows.form.elements code. Any ideas how I can get it to work in Firefox?

    <script type="text/javascript" language="JavaScript">
    <!--
    function formValidation(form){
    if(notEmpty(window.form.elements['Last Name'])){
    if(notEmpty(form.Organization)){
    if(notEmpty(form.Country)){
    if(notEmpty(window.form.elements['E-Mail'])){
    window.open( "http://www.yahoo.com", "thankyouPopup",
    "status = 0, toolbar = 0, location = 0, menubar = 0, directories = 0, height = 300, width = 300, resizable = 0" );
    document.form.Send.disabled=true;document.form.Send.value='Sending details (please wait)';
    return true;
    }
    }
    }
    }
    return false;
    }

    function notEmpty(elem){
    var str = elem.value;
    if(str.length == 0){
    alert("Usted debe rellenar al menos las casillas con rótulos AZULES para enviar el formulario.");
    return false;
    } else {
    return true;
    }

    }

    //-->
    </script>

    Cheers!

  4. #4
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BigKingy
    Any ideas how I can get it to work in Firefox?
    Yes, don't use invalid characters (like space) in your name attributes!
    Birnam wood is come to Dunsinane

  5. #5
    SitePoint Enthusiast BigKingy's Avatar
    Join Date
    Jun 2004
    Location
    London
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AutisticCuckoo
    Yes, don't use invalid characters (like space) in your name attributes!
    The thing is, the form data gets submitted into a database, and so the field names have to have spaces inbetween, so Last Name instead of LastName.

    Is there any possible way to get this to work in Firefox?

  6. #6
    SitePoint Enthusiast BigKingy's Avatar
    Join Date
    Jun 2004
    Location
    London
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have just tested it in Opera and it works fine.

    So its only Firefox that isnt playing ball. Is there not a work around to get it working in Firefox?

    Any assistance on this would be most appriciated.

    Thanks!

  7. #7
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe you could refer to the Last Name field by number instead of by name. Like form.elements[2].


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •