views:

319

answers:

4
<script type="text/javascript">
function validate() {
    if (document.form.price.value.trim() === "") {
        alert("Please enter a price");
        document.form.price.focus();
        return false;
    }
    if (document.form.price.value !== "") {
        if (! (/^\d*(?:\.\d{0,2})?$/.test(document.form.price.value))) {
            alert("Please enter a valid price");
            document.form.price.focus();
            return false;
        }
    }
    return true;
}
</script>

<form action="" method="post" name="form" id="form" onsubmit="return validate(this);">

<input name="price"  type="text" class="r2" />
<input name="price2" type="text" class="r2" />
<input name="price3" type="text" class="r2" />
<input name="price4" type="text" class="r2" />
<input name="price5" type="text" class="r2" />
...more....
<input name="price50" type="text" class="r2" />

Current javascript code working fine to validate the name="price".

Question : How to make the code will working as global validation? Example can validate the price, price2, price3, price4, price5 etc.. with a single function. Let me know :)

A: 

You can validate all 5 prices and return true only if all 5 match your validation rules.

DmitryK
+4  A: 

My personal recommendation would be something like this:

<script type="text/javascript">
function validate() {
    if (validatePrice(document.form.price) && 
        validatePrice(document.form.price2) && 
        validatePrice(document.form.price3) && 
        validatePrice(document.form.price4) && 
        validatePrice(document.form.price5))
       return true;
    else
       return false;
}

function validatePrice(price)
{
    if (price.value.trim() === "") {
        alert("Please enter a price");
        price.focus();
        return false;
    }
    if (price.value !== "") {
        if (! (/^\d*(?:\.\d{0,2})?$/.test(price.value))) {
            alert("Please enter a valid price");
            price.focus();
            return false;
        }
    }
    return true;       
}
</script>
Brisbe42
How about using an array instead of a large if statement?
strager
Thanks Brisbe42, work like a charm. There have another option to validate the input only? such a $("input")
bob
return validatePrice(document.form.price)
DmitryK
Yes, you would just do validatePrice(document.form.price.value.trim()) for each of those. This would, naturally, entail then removing the price.focus() in each if statement in validatePrice, to make it work correctly, and change the if statements to just be if (price === "") and if (price !== "").
Brisbe42
A: 

The easiest in this case is really to use jQuery. This way you can use a generic selector and apply the validation on all items.

$("#price*").each(function() {//Do your validation here $(this) is the item price, then price2 then price3})

For anything else you would need to query the DOM and then that doesn't work the same in all browsers.

Today, you can't really do anything in Javascript and ignore something like jQuery http://docs.jquery.com/ or Scriptalicious.

Philippe Asselin
+2  A: 

If you do not plan on using jQuery this should work.

function validate() {
    for (var field in document.getElementsByTagName('input')) {
        if (isPriceField(field)) {
            field.value = field.value.trim();
            if (isNaN(parseFloat(field.value))) {
                return alertAndFocus(field, "Please enter a valid price");
            }
        }         
    }

    return true;
}

function isPriceField(field) {
    return (field.name.substr(0, Math.min(5, field.name.length)) === 'price')
}

function alertAndFocus(field, message) {
    alert(message);
    field.focus();
    return false;
}
ChaosPandion