Board index   FAQ   Search  
Register  Login
Board index php forum :: HTML JavaScript

Javascript Validation

Javascript coding ..

Moderators: macek, egami, gesf

Javascript Validation

Postby garr1095 » Mon Apr 23, 2012 11:46 am

Greetings,

I have a web page that has a form for input. The form also uses php to concat the string "employee_" with $i, like so:
Code: Select all
<input type='text' name='clockIn_<?php echo $i; ?>' size='8' />

Which works fine. I can insert the times into my MySQL tables. I want to verify that a time has been entered into the field. I am using this:
Code: Select all
if (dlrDate==null || dlrDate=="")
{
alert("You must enter a date.");
return false;
}

to validate that a date has been entered. When I try to do:
Code: Select all
for (i=1;i<11;i++)
{
if (clockIn_ + i==null || clockIn_ + i=="")
...

it doesn't work. Can anyone point me in the right direction?
As you can tell, I am a beginner.

Thanks!
garr1095
New php-forum User
New php-forum User
 
Posts: 7
Joined: Wed Apr 18, 2012 3:21 pm

Re: Javascript Validation

Postby gandahar » Fri Apr 27, 2012 5:10 pm

hi !

it's not working because the name of the variable can't be completed by other variable, if you want to do this, try jQuery, you have lots of selector option


//try to put an id it will be much easier
//(i added a class to the input field for jQuery example)
Code: Select all
<input type='text' id='clockIn_<?php echo $i; ?>' name='clockIn_<?php echo $i; ?>' size='8' class='ab' />



//javascript
Code: Select all
for (i=1;i<11;i++){
    if (document.getElementById('clockIn_'+i).value==null || document.getElementById('clockIn_'+i).value=="")


//jQuery example using class selector
Code: Select all
$('.ab').each(function(){
   if ($(this).val()==''){//do something }
});
User avatar
gandahar
New php-forum User
New php-forum User
 
Posts: 112
Joined: Tue Mar 15, 2011 4:05 am

Re: Javascript Validation

Postby minimihi » Sat Apr 28, 2012 9:47 am

To begin with, are you assigning form element value to variable?

Also
Code: Select all
if (clockIn_ + i==null || clockIn_ + i=="")
would give an error.
Check your Console to see JavaScript errors :-x even IE has it.

should be:
Code: Select all
if (('clockIn_' + i) == null || ('clockIn_' + i) == "")
User avatar
minimihi
New php-forum User
New php-forum User
 
Posts: 238
Joined: Sat Apr 14, 2012 11:57 am
Location: Vilnius, Lithuania


Return to JavaScript

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron