+ Reply to Thread
Results 1 to 5 of 5

Thread: javascript problem

  1. #1
    Barn Newbie yangski is an unknown quantity at this point yangski's Avatar
    Join Date
    Jul 2009
    Posts
    26
    Rep Power
    3

    javascript problem

    I have this two images:

    image1:


    image2:


    My problem here is that every time i click the submit button, the error messages just adds up to the previous set of error messages. how can I prevent this from happening? What I want to do is that every time I click the submit button, the javascript would only show the current set of error messages... deleting or disregarding the previous one...

    this is my code - TextValidation.js (a seperate javascript file):

    Code:
    //Text Validation for Product Details
    var numericExpression =  /^[0-9]+$/;
    var error = "";
    function validate_Product()
    {
        validate_ProductName();
        validate_ProductPrice();
        validate_StockUnit();
        validate_reorderPt();
        validate_Weight();
        validate_Tax();
        validate_Description();
    
        if (error != "")
        {
            alert(error);
            return false;
        }
    
    }
    function validate_ProductName()
    {
    //    debugger;
        var name = document.getElementById('ctl00$ContentPlaceHolder1$txtProductName').value;
        if(name == '')
        {
            error = error + '\nProduct Name: Please enter a Product Name';
        }
        else if (name.length >50)
        {
            error = error + '\nProduct Name: You\'re only allowed to enter up to 50 characters.';
        } 
       
    }
    
    function validate_ProductPrice()
    {
        var price = document.getElementById('ctl00$ContentPlaceHolder1$txtPrice').value;
        if (price == '')
        {
            error = error + '\nProduct Price: Please enter the product price.';
        }
        else if (price.match(!numericExpression))
        {
            error = error + '\nProduct Price: Please enter numeric values only.';
        }
    }
    
    function validate_StockUnit()
    {
        var stockUnit = document.getElementById('ctl00$ContentPlaceHolder1$txtUnitId').value;
        if (stockUnit == '')
        {
            error = error + '\nStock Unit: Please enter a Stock Unit.';
        }
        else if (stockUnit.match(!numericExpression))
        {
            error = error + '\nStock Unit: Please enter numeric values only.';
        }
    }
    
    function validate_reorderPt()
    {
        var reorderPt = document.getElementById('ctl00$ContentPlaceHolder1$txtReorderPt').value;
        if (reorderPt == '')
        {
            error = error + '\nReorder Point: Please enter a reorder point for the product.';
        }
        else if (reorderPt.match(!numericExpression))
        {
             error = error + '\nReorder Point: Please enter numeric values only.';
        }
    }
    
    function validate_Weight()
    {
        var weight = document.getElementById('ctl00$ContentPlaceHolder1$txtWeight').value;
        if (weight == '')
        {
            error = error + '\nProduct Weight: Please enter the weight of the product.';
        }
        else if(weight.match(!numericExpression))
        {
            error = error + '\nProduct Weight: Please enter numeric values only.';
        }
    }
    
    function validate_Tax()
    {
        var tax = document.getElementById('ctl00$ContentPlaceHolder1$txtTax').value;
        if (tax == '')
        {
             error = error + '\nTax Amount: Please enter a price value for the product.';
        }
        else if(tax.match(!numericExpression))
        {
             error = error + '\nTax Amount: Please enter numeric values only.';
        }
    }
    
    function validate_Description()
    {
        var description = document.getElementById('ctl00$ContentPlaceHolder1$txtDesc').value;
        if(description.length>250)
        {
            error = error + '\nProduct Description: You have reached beyond the character limit.';
        }
    }
    
    Attached Images

  2. #2
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Change the lines that say
    Code:
    error = error + '\nProduct Name: Please enter a Product Name';
    
    to
    Code:
    error = '\nProduct Name: Please enter a Product Name';
    
    And switch the order you call the validation functions so the one relating to the first form element is called last.

  3. #3
    Ask Me About Dragons :D Shadow Wizard has a spectacular aura about Shadow Wizard has a spectacular aura about Shadow Wizard's Avatar
    Join Date
    Jul 2008
    Location
    Israel
    Posts
    795
    Blog Entries
    2
    Real Name
    Yahav
    Rep Power
    5

    Quote Originally Posted by richyrich View Post
    Change the lines that say
    Code:
    error = error + '\nProduct Name: Please enter a Product Name';
    
    to
    Code:
    error = '\nProduct Name: Please enter a Product Name';
    
    And switch the order you call the validation functions so the one relating to the first form element is called last.
    nope, that's won't do what he asked it will show only one line.
    to avoid adding over and over, just add the line in bold to your function:
    Code:
    function validate_Product()
    {
        error = "";
        validate_ProductName();
    ......
    }
    
    this will "clean" the variable value in each execution of the validation.

  4. #4
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Quote Originally Posted by Shadow Wizard View Post
    nope, that's won't do what he asked it will show only one line.
    to avoid adding over and over, just add the line in bold to your function:
    Code:
    function validate_Product()
    {
        error = "";
        validate_ProductName();
    ......
    }
    
    this will "clean" the variable value in each execution of the validation.
    I thought that's what he wanted. ie if one box doesn't validate then the error just says that box doesn't validate and then stops.

    <edit>Oh, I think I see....He means each time the submit button is clicked, any previous error messages aren't cleared..</edit>

  5. #5
    Ask Me About Dragons :D Shadow Wizard has a spectacular aura about Shadow Wizard has a spectacular aura about Shadow Wizard's Avatar
    Join Date
    Jul 2008
    Location
    Israel
    Posts
    795
    Blog Entries
    2
    Real Name
    Yahav
    Rep Power
    5

    Quote Originally Posted by richyrich View Post
    I thought that's what he wanted. ie if one box doesn't validate then the error just says that box doesn't validate and then stops.

    <edit>Oh, I think I see....He means each time the submit button is clicked, any previous error messages aren't cleared..</edit>
    yep, simple case of "miscleaning".

+ Reply to Thread

Similar Threads

  1. line breaks in javascript
    By yangski in forum JavaScript Programming
    Replies: 2
    Last Post: July 26th, 2009, 08:34 PM
  2. JavaScript Date Picker problem??
    By Centurion in forum JavaScript Programming
    Replies: 5
    Last Post: March 20th, 2009, 09:58 AM
  3. JavaScript Tutorial
    By Shadow Wizard in forum JavaScript Programming
    Replies: 0
    Last Post: December 2nd, 2008, 07:33 AM
  4. calling javascript function from asp
    By guddu in forum ASP Development
    Replies: 8
    Last Post: November 19th, 2008, 02:48 PM

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

SEO by vBSEO