Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Feb 2005
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Form Validation using getElementbyId

    i have modifed a script by Stephen Poley and have got it to work correctly:
    http://www.xs4all.nl/~sbpoley/webmatters/formval.html

    However, i need the page to be in Strict XHTML 1.1 so i can't do <form name="contactform">. When i try and use the getElementbyId('name') instead of document.forms.contactform.name the script doesnt work.

    what am i doing wrong?

    Code:
    <script type="text/javascript">
    <!--
    // Only script specific to this form goes here.
    // General-purpose routines are in a separate file.
      function validateOnSubmit() {
        var elem;
        var errs=0;
        // execute all element validations in reverse order, so focus gets
        // set to the first one in error.
        if (!ValidateOther(document.getElementbyId('name'), 'nameinfo'))        errs += 1;
        if (!validateEmail(document.getElementbyId('email'), 'emailinfo', true)) errs += 1; 
        if (!ValidateOther(document.getElementbyId('subject'), 'subjectinfo'))        errs += 1;
        if (!ValidateOther(document.getElementbyId('message'), 'messageinfo'))        errs += 1; 
        if (errs>1)  alert('There are fields which need correction before sending');
        if (errs==1) alert('There is a field which needs correction before sending');
    
        return (errs==0);
      };
    // -->
    </script>
    
    <form onsubmit="return validateOnSubmit()" action="http://www.cems.uwe.ac.uk/~tdrewry/cgi/cgiform.cgi" method="post">
    
     <p>
       <label for="name">*Name:</label><br />
       <input type="text" name="name" id="name" onchange="ValidateOther(this, 'nameinfo');" />
       <label id="nameinfo">&nbsp;</label><br />
     </p>
    
     <p>
       <label for="email">*Email:</label><br />
       <input type="text" name="email" id="email" onchange="validateEmail(this, 'emailinfo', true);" />
       <label id="emailinfo">&nbsp;</label><br />
     </p>
    
     <p>
       <label for="subject">*Subject:</label><br />
       <input type="text" name="subject" id="subject" onchange="ValidateOther(this, 'subjectinfo');" />
       <label id="subjectinfo">&nbsp;</label><br />
     </p>
    
     <p>
       <label for="message">*Message:</label><br />
       <textarea name="message" id="message" onchange="ValidateOther(this, 'messageinfo');" cols="255" rows="9"></textarea>
       <label id="messageinfo">&nbsp;</label><br />
     </p>
    
     <p>
       <input type="submit" name="Submit" value="Send" />
     </p>
    
    </form>
    the whole page can be found here

  • #2
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    In what way does it not work?
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #3
    New Coder
    Join Date
    Feb 2005
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if you click on the link
    http://www.thelogoarchive.com/dylan/contact.html and press send with invalid data it is suposed to pop up with a box saying "There are fields which need correction before sending".

    if you save the file to your computer and change the getelimentbyid to forms.contact.*insert value such as name* and put a name=contact thing in the form tag it will work.

    but strict xhtml 1.0 doesnt suport name inside a form tag.

  • #4
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    JavaScript is case-sensitive.

    There is no
    Code:
    document.getElementbyId()
    you mean
    Code:
    document.getElementById()
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #5
    New Coder
    Join Date
    Feb 2005
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ahh brilliant,

    thanks


  •  

    Posting Permissions

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