Jump to content

[SOLVED] cn1.value =="" .. cn2.value="" convert into for loop (field validation)?


sayedsohail

Recommended Posts

HI everyone,

 

I got six variable cn1 to cn6, would like to check the value of the fields using for loop and alert the user if any of the cn1 to cn6 fields are empty here is my code?  How do i convert this into for loop?

 

if ((cn1.value =="") || (cn1.value == null) || isblank(cn1.value) || (cn2.value =="") || (cn2.value == null) || isblank(cn2.value) )
{
alert("Please enter all the required fields");
}

are you using id tags for the fields?

you could just do something like this

 

<input id="cn1" name="cn1" type="text" />
<script>
//if you're using id attribute
for(var i=0;i<6;i++){
    document.getElementById("cn"+i).value
}
////if you're using name attribute
for(var i=0;i<6;i++){
    document.getElementByName("cn"+i).value
}
</script>

var cn1 =  document.getElementById('cname').value;

var cn2 =  document.getElementById('address1').value;

 

This is how i am storing the value to cn1 to cn6

But how do i check the value for all the 6 ellements and than alert the user, if any of the fields are empty?  Please.

var fields = [

document.getElementById('cname').value,

document.getElementById('address1').value

];

 

var error = false;

var error_msg = "";

 

for(c in fields){

if(fields[c] == ''){

error = true;

error_msg = fields[c].name + ' is blank.\n';

}

}

 

if(error){

alert(err_msg);

}

there were some errors with my orig post

 

var fields = [

document.getElementById('cname'),

document.getElementById('address1')

];

 

var error = false;

var error_msg = "";

 

for(c in fields){

if(fields[c].value == ''){

error = true;

error_msg += fields[c].name + ' is blank.\n';

}

}

 

if(error){

alert(err_msg);

}

 

#edit

or better yet, give each field the title attribute and put in a descriptive field title like title="User Name"

 

and change this to

error_msg += fields[c].title + ' is blank.\n';

var fields = [document.getElementById('cname').value,

document.getElementById('address1').value,

document.getElementById('postcode').value,

document.getElementById('city').value,

document.getElementById('phone').value,

document.getElementById('mobile').value

];

 

var error = false;

var error_msg = "";

// this is what i changed but its not working//

for(c in fields){

if(fields[c] == '' || fields[c] == null || isblank(fields[c])){

error = true;

error_msg = fields[c].name + ' is blank.\n';

}

}

 

if(error){

alert(err_msg);

}

else

{

with null you'll need the === operator as it is a datatype not a string or integer

 

isblank() a custom function?

 

and use my correction where i took the value out of the defining array so that you can access more of the element's properties inside of the loop

I kept the original revised code , but still its not working although the fields are there.

 

var fields = [

document.getElementById('cname'),

document.getElementById('address1')

];

 

var error = false;

var error_msg = "";

 

for(c in fields){

if(fields[c].value == ''){

error = true;

error_msg += fields[c].name + ' is blank.\n';

}

}

 

if(error){

alert(err_msg);

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.