JaiSat
JaiSat

Reputation: 2386

Is there any better way for non-empty validation?

I've been validating input elements to ensure each element at-least have some data and notify user, what field is to be filled. The way I'm doing is,

function validate()
{   
    var product_name  = document.getElementById('pname').value;
    var p_number      = document.getElementById('pnumber').value;
    var description   = document.getElementById('descr').value;

    if(product_name=="")
         {alert("Please fill Product Name");   return false;}
    else if(p_number=="")
         {alert("Please fill product Number"); return false;}
    else if(description=="")
         {alert("Please fill description");    return false;}
    else
         return true;

}

I'm feeling that this code is not optimum and need to be improved. You guys might have be experienced on this validation and have some tricks around. Please help me to do the same validation in a better way. Thanks for your time.

Upvotes: 0

Views: 60

Answers (4)

Vicky Gonsalves
Vicky Gonsalves

Reputation: 11717

Use:

function validate()

{   
    var product_name  = document.getElementById('pname').value;
    var p_number      = document.getElementById('pnumber').value;
    var description   = document.getElementById('descr').value;

    if((product_name.trim()).length == 0)
         {alert("Please fill Product Name");   return false;}
    else if((p_number.trim()).length == 0)
         {alert("Please fill product Number"); return false;}
    else if((description.trim()).length == 0)
         {alert("Please fill description");    return false;}
    else
         return true;

}

Upvotes: 0

kavun
kavun

Reputation: 3381

You should make your validate function more generic so that it can be used for any number of fields. You should also use trim() to guard against empty spaces.

function validate(fields) {   
    for (var i = 0, l = fields.length; i < l; i++) {
        var field = document.getElementById(fields[i].id);
        if (field.value == null || field.value.trim() === '') {
            alert('Please fill in ' + fields[i].name);
            return false;
        }
    }
    return true;
}

Then to validate, you would pass in the names and ids

var isValid = validate([
    { id: 'pname', name: 'Product Name' },
    { id: 'pnumber', name: 'Product Number' },
    { id: 'descr', name: 'Description' }
]);

Upvotes: 3

Kathir
Kathir

Reputation: 1240

Trim function remove whitespace from both sides of a string. It's used for validating the empty spaces in the field.

So try using this format

if(product_name.trim().length == 0)

Upvotes: 0

Ankit Tyagi
Ankit Tyagi

Reputation: 2375

Here try this :

function validate()
{   
    var d = document;
    var byId = function( id ) { return d.getElementById( id ); };

    var product_name  = byId('pname').value;
    var p_number      = byId('pnumber').value;
    var description   = byId('descr').value;

    if(product_name=="")
         {alert("Please fill Product Name");   return false;}
    else if(p_number=="")
         {alert("Please fill product Number"); return false;}
    else if(description=="")
         {alert("Please fill description");    return false;}
    else
         return true;

}

Upvotes: 0

Related Questions