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");
}

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);

}

Link to comment
Share on other sites

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';

Link to comment
Share on other sites

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

{

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.