SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2010
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript form validation problem

    I am having a problem with validating my form, it works if I only validate 1 field, but if I try to validate more than 1, nothing happens when I click submit

    Here is my code:

    function validateForm()
    {
    var x=document.forms["dform"]["name"].value
    if (x==null || x=="")
    {
    alert("Enter Name!");
    return false;
    }
    }
    {
    var x=document.forms["dform"]["selling_1"].value
    if (x==null || x=="")
    {
    alert("Enter Selling Name!");
    return false;
    }
    }

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    The return statement stops all code after it from being able to execute.

    Instead of returning at the first sight of an error, you may want to set a variable at the start called isValid to true, and then set isValid to false when you encounter an error.

    At the end of things, you can then return that isValid variable.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2010
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks! Do you mean like this:

    function validateForm()
    isValid=true;
    {
    var x=document.forms["dform"]["name"].value
    if (x==null || x=="")
    {
    alert("Enter Name!");
    isValid= false;
    }
    }
    {
    var x=document.forms["dform"]["selling_1"].value
    if (x==null || x=="")
    {
    alert("Enter Selling Name!");
    isValid= false;
    }
    }

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Nearly. No code goes before the function brace, so it should start with:


    Code javascript:
    function validateForm()
    {
        isValid=true;
        ...
    }

    Many of is prefer to put the brace immediately after the function parenthesis, to help prevent such easy mistakes.

    Code javascript:
    function validateForm() {
        isValid=true;
        ...
    }

    And it should end by returning the isValid variable.

    Code javascript:
    function validateForm() {
        ...
        return isValid;
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2010
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks heaps! I tried this, but when I submit the form it doesnt do anything.. no alert message appears

    function validateForm()
    {
    isValid=true;
    var x=document.forms["dform"]["name"].value
    if (x==null || x=="")
    {
    alert("Enter Name!");
    return isValid;
    }
    }
    {
    isValid=true;
    var x=document.forms["dform"]["selling_1"].value
    if (x==null || x=="")
    {
    alert("Enter Selling Name!");
    return isValid;
    }
    }

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Do you know about indenting code? If you indent your code, you will see where the problem lies.

    You can wrap your code in [code] tags, so that the indents will remain. For example, this code:

    Code:
    [code]
    var function foo {
        foo = 'bar';
        return foo + 'baz';
    }
    [/code]
    will result in:

    Code:
    function foo () {
        var foo = 'bar';
        return foo + 'baz';
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •