SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Error in Form Validation script

    I have been looking at this code over and over again now and I just cannot figure out where I went wrong.

    This formval function is actually longer but this is the first 'check' it does for the first input tag I have.
    The next check it does works perfectly.
    The only difference between them is that this one checks for letters and the other one checks for numbers.

    Code:
    function formval() {
      var letters = /^[a-zA-Z]+$/;
     
      if (document.getElementById('name').value.match(letters)) {
        return true;
      }
      else {
        alert('Please fill in your name');
        document.getElementById('name').focus();
        return false;
      }
    }
    Which belong to:

    HTML Code:
    <input type="text" name="Name: " id="name">

  2. #2
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try using regexp.test() instead.
    Code:
    if(letters.test(document.getElementById('name').value)) {
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  3. #3
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    regexp.test () is unfamiliar to me.
    I tried using it anyway by replacing
    Code:
    if (document.getElementById('name').value.match(letters)) {
    with
    Code:
    if(letters.test(document.getElementById('name').value)) {
    which didn't work.

    Then I also switched out
    Code:
    function formval() {
    with
    Code:
    regexp.test()
    which didn't work either.

  4. #4
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use regexp.test() a lot more than I use string.match().

    Which browsers are you testing in?

    We'll need to see more of your code to be able to help you further.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  5. #5
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm using IE.

    Let's start with the html document so I can show that the linking is done correctly.
    HTML Code:
    <html>
    <head>
    <title>title</title>
    <link href="the.css" rel="stylesheet" type="text/css" />
    <script src="the.js">
    </script>
    </head>
    <body>
    <!-- skipping some code here that has nothing to do with the form -->
    <div id="main_div">
    <form>
    <br /><br /><br />
    <table>
    <tr><td>Name: </td><td><input type="text" name="Name: " id="name"></td></tr>
    <tr><td>Age: </td><td><textarea name="Age: " rows="1" cols="3" id="age"></textarea></td></tr>
    <!-- Skipping some more inputs -->
    <input type="button" value="Send" onclick="formval()">
    </form>
    </div>
    </body>
    </html>
    And now the.js
    Code:
    function formval() {
      var numbers = /^[0-9]+$/;
      var letters = /^[a-zA-Z]+$/;
    
      
      if (document.getElementById('name').value.match(letters)) {
        return true;
      }
      else {
        alert('Please Fill in your name');
        document.getElementById('name').focus();
        return false;
      }
    /* The above part is what ain't working */
      else if (document.getElementById('age').value.match(numbers)){
        return true;
      }
      else {
        alert('Please fill in your age');
        document.getElementById('age').focus();
        return false;
      }
    }
    The second check(numbers) works perfectly if I were to comment out the first check(letters).

  6. #6
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there a form control with the ID of "level"?

    I still don't see anything to keep it from working. Your code could be improved substantially though.

    I recommend that you use Firefox's Error/JavaScript Console to check your script for errors.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  7. #7
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, Was having long loading times with the edit feature.
    level was ment to be age.

    I will download Firefox now and give it a try.

  8. #8
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I got the first part(letters) to work now.

    I edited out the 'else' from
    Code:
    else if (document.getElementById('age').value.match(numbers)) {
    so it became
    Code:
    if (document.getElementById('age').value.match(numbers)) {
    Now the age check is not working.

  9. #9
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's because you've got return statements in both of the if/else blocks from the previous pair.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  10. #10
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for your help, the whole formval() function works great now!
    There is another problem I have related to this..

    What command in javascript is used to send the form away in email?
    I tried changing the form button to type="submit".
    and put the formval() function inside the form tag in onSubmit="".
    HTML Code:
    <form onSubmit="formval()" action="mailto:theemailadress@something.com" method="post" enctype="text/plain">
    <input type="submit" value="send">
    </form>
    If I do this I get a popup after the alert(if they dont fill in the form inputs).
    the popup is about "your about to send an email, continue?" I don't know how or if it's possible to remove that popup but I would atleast like it to not popup unless the person had filled in the whole form correctly.

  11. #11
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Before using mailto:, I recommend you read this article.

    Which server-side languages do you have available on your server?
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  12. #12
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wow, That was a real good article.
    So I would have to use a server side language like PHP for e.g to go around the problem.

    My server can handle MS SQL (database) and asp, asp.net scripts.
    I don't know how to programm in those yet though.

  13. #13
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So you're using IIS? I prefer the LAMP (Linux, Apache, MySQL, PHP and/or Perl) option myself.

    Google finds a lot of information on ASP form mail scripts.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.


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
  •