Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 757
  • Last Modified:

Wildcard form input names? Simple.

I need this to allow for XXX to be any 3 char set. I may have MonHoliday, TueHoliday, WedHoliday, etc.

 var Holiday=form.XXXHoliday.value*1;


I tried

 var Holiday=form./Holiday/.value*1;

but obviously thats incorrect.

Help is appreciated.
0
VAN
Asked:
VAN
  • 6
  • 4
  • 3
  • +4
5 Solutions
 
peh803Commented:
I believe I can help, but I would like to know more.  What are some valid examples?  Any 3 characters should be allowed, while anything else is an error?

peh803
0
 
VANAuthor Commented:
Mon, Tue, Wed, Thu, Fri, Sat, Sun

Those would be acceptable.
0
 
peh803Commented:
<script language="javascript">
<!--
function checkIt(){

  var holiday = document.getElementById('holiday').value;
  if(holiday.length!=3){
    alert('Invalid entry!! Please enter a 3 digit value. \n Current Length: '+holiday.length)
  }
  else
  {
    alert('Your entry is looking good...');
  }
}
-->
</script>

<input type=text name=holiday id=holiday>
<input type=button onclick="checkIt();" value="check value">
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
justinbilligCommented:
<script language="javascript">

function GetValue( )
{
      var astrDays = Array( "Mon", "Teu", "Wed", "Thur", "Fri", "Sat", "Sun" );
      var intIndex = 0;
      var intArrayInidex = 0;
      var strDay = "";


      // Loop through
      for( intIndex = 0; intIndex < frmTest.elements.length - 1; intIndex++ )
      {
            
            // Is this a control?
            if( frmTest.elements[ intIndex ].type == "text" )
            {
                  // Loop through through the day array
                  for( intArrayIndex = 0; intArrayIndex < astrDays.length; intArrayIndex++)
                  {
                        strDay = frmTest.elements[ intIndex ].id;
                        
                        // Is this the control we need?
                        if( strDay.substr( 0, 3 ) == astrDays[ intArrayIndex ] )
                        {
                              alert( strDay + ": " + frmTest.elements[ intIndex ].value );
                        }
      
                  }
            }
      }
}


</script>
<html>


      <body>

            <form name="frmTest">
            <input type="text" name="MonHoliday" id="MonHoliday" value="">
            <input type="text" name="TeuHoliday" id="TeuHoliday" value="">
            <input type="button" name="btnGetValue" value="Get Value" onclick="GetValue( );">
            </form>
      </body>
</html>

is this more of what you needed?
0
 
VANAuthor Commented:
Let me try and clarify...

I have a form already built with the 7 days of the week. These inputs are named SunWorked, MonWorked, etc

In the event of a holiday, the input on that day gets named XXXHoliday. (where XX is the day prefix)

My script needs to first see IF there are any inputs named XXXholiday, and IF there are, then I need to grab the values using something like:

var Holiday=form.XXXHoliday.value*1; (again, where XXX is the day prefix Sun, Mon, Tue, etc)

Does this help?
0
 
justinbilligCommented:
if that is hwat you need you only need to make sure that the controls you are looking for have the same name and id
0
 
ColosseoCommented:
Hi Van

what you are probably after is somthing like this

var day_Of_Week = "Mon"

var Holiday=document.getElementById(day_Of_Week + "Holiday").value*1;

HTH

Scott
0
 
StormyWatersCommented:
This function will get the value of ***Holiday and alert it. You can do whatever you want with the value, of course. If ***Holiday matches more than 1 field holiday will contain the last one. I can modify it to contain an array if you'd like.
(*** can be whatever you mentioned above)

function getHoliday(form) {
  var valid = new Array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun");
  var regexp = new RegExp( "(" + valid.join("|") + ")Holiday");
  var holiday;
  for(var i = 0; i < form.elements.length; i++) {
    if(regexp.test(form.elements[i].name)) {
        holiday = form.elements[i].value;
      }
  }
  alert(holiday);
}

pass the form name to it, ie document.myform
0
 
peh803Commented:
how about something like this:

<script language="javascript">
<!--
function checkIt(){

  for(intIndex=0;intIndex<myForm.elements.length-1; intIndex++)
    {
      strDay = myForm.elements[ intIndex ].id;
      strDay.substr(3).toLowerCase()
      if( strDay.substr(3).toLowerCase() == 'holiday' )
      {
        alert( strDay + ": " + myForm.elements[ intIndex ].value );
      }
    }
}
-->
</script>
<form name=myForm id=myForm>
<input type=text name=MonHoliday id=MonHoliday value="Monday"><BR>
<input type=text name=TueHoliday id=TueHoliday value="Tuesday"><BR>
<input type=text name=WedHoliday id=WedHoliday value="Wednesday"><BR>
<input type=text name=ThuHoliday id=ThuHoliday value="Thursday"><BR>
<input type=text name=FriHoliday id=FriHoliday value="Friday"><BR>
<input type=text name=SatHoliday id=SatHoliday value="Saturday"><BR>
<input type=text name=SunHoliday id=SunHoliday value="Sunday"><BR>
<input type=button onclick="checkIt();" value="check value">
</form>

regards,
peh803
0
 
peh803Commented:
Van -- Obviously the form in my previous post is just to show an example of the function working.  You can adjust the implementation and use it however you like...putting the values of holiday inputs into a javascript array or whatever else you want...

let me know if you have any other questions.

regards,
peh803
0
 
VANAuthor Commented:
Here's the existing script. This should help.

================
{
     var PerDayMax=8;
     var PerWeekMax=40;
     var Regular=0;
     var Overtime=0;

///// NEED HELP HERE
    var Holiday may or may not exist in the form. if not, assign 0 value, if so, grab value from form
     var Holiday = form.XXXHoliday.value;
/////////////////////////

     for(i=0;i<form.elements.length;i++)
    {
         if(form.elements[i].name.match(/Worked/))
         {

                   if ((Regular+Holiday) > PerWeekMax){
          form.elements[i+1].value=0;
          form.elements[i+2].value=form.elements[i].value*1;

              }
              else if((Holiday*1+Regular+form.elements[i].value*1)>PerWeekMax)
              {
                form.elements[i+2].value=(form.elements[i].value*1)-(PerWeekMax-(Regular+Holiday*1));
                form.elements[i+1].value=form.elements[i].value - form.elements[i+2].value ;          
               }

              else if((form.elements[i].value*1)>PerDayMax)
              {
            form.elements[i+2].value=form.elements[i].value*1-PerDayMax;
            form.elements[i+1].value=form.elements[i].value*1-form.elements[i+2].value*1;
              }
              else
              {
                   form.elements[i+1].value=form.elements[i].value;
                   form.elements[i+2].value='';
              }
              Regular+=form.elements[i+1].value*1;
              Overtime+=form.elements[i+2].value*1;
             
          }
    }
     form.TtlREG.value=Regular
     form.TtlOT.value=Overtime
      if (form.TtlOT.value == 0)
      {
      form.TtlOT.value = '';
      }
}
==================
0
 
peh803Commented:
try putting this in there....

  //initialize holiday to 0  
  var holiday = 0;
  for(intIndex=0;intIndex<myForm.elements.length-1; intIndex++)
    {
      strDay = myForm.elements[ intIndex ].id;
      strDay.substr(3).toLowerCase()
      if( strDay.substr(3).toLowerCase() == 'holiday' )
      {
        //if any holidays are found, add them to the holiday variable...
        holiday = holiday + (myForm.elements[ intIndex ].value*1);
      }
    }

regards,
peh803
0
 
DexstarCommented:
@Van:

These guys are on the right track with the searching for "holiday" instead of the days of the week.  However, they need more checking to give you the results that you need.  Here is the test page that I made... Try it and see if it does what you want.

-D*

===

<HTML>
      <HEAD>
            <script language="JavaScript">
                  function GetHolidays( objForm )
                  {
                        var nHolidays = 0;
                        var nValue;
                        
                        for ( var nIndex = 0 ; nIndex < objForm.elements.length-1 ; ++nIndex )
                        {
                              // Search for "Holiday", not case sensitive
                              if ( objForm.elements[nIndex].id.search(/holiday/i) != -1 )
                              {
                                    nValue = parseInt(objForm.elements[nIndex].value);
                                    if ( !isNaN(nValue) )
                                    {
                                          nHolidays += nValue;
                                    }
                              }
                        }
                        
                        return nHolidays;
                  }
            </script>
            <TITLE>Get Holidays Test Page</TITLE>
      </HEAD>
      <BODY>
            <form name="frmHolidays">
                  Mon<input type="text" name="MonHoliday" id="MonHoliday" value=""><br>
                  Tue<input type="text" name="TueWorked" id="TueWorked" value=""><br>
                  Wed<input type="text" name="WedWorked" id="WedWorked" value=""><br>
                  Thu<input type="text" name="ThuWorked" id="ThuWorked" value=""><br>
                  Fri<input type="text" name="FriHoliday" id="FriHoliday" value=""><br>
                  Sat<input type="text" name="SatWorked" id="SatWorked" value=""><br>
                  Sun<input type="text" name="SunWorked" id="SunWorked" value=""><br>
                  <input type="button" name="Holidays" value="Show Holidays"
                        onclick="ShowHolidays.innerHTML = GetHolidays(frmHolidays);">
            </form>
            <div id="ShowHolidays"></div>
      </BODY>
</HTML>
0
 
justinbilligCommented:
<script language = "javascript">
var g_intHoliday = 0;

function GetValue( )
{
     var astrDays = Array( "Mon", "Teu", "Wed", "Thur", "Fri", "Sat", "Sun" );
     var intIndex = 0;
     var intArrayInidex = 0;
     var strDay = "";


     // Loop through
     for( intIndex = 0; intIndex < frmTest.elements.length - 1; intIndex++ )
     {
         
          // Is this a control?
          if( frmTest.elements[ intIndex ].type == "text" )
          {
               // Loop through through the day array
               for( intArrayIndex = 0; intArrayIndex < astrDays.length; intArrayIndex++)
               {
                    strDay = frmTest.elements[ intIndex ].id;
                   
                    // Is this the control we need?
                    if( strDay.substr( 0, 3 ) == astrDays[ intArrayIndex ] )
                    {
                         g_intHoliday += ( frmTest.elements[ intIndex ].value * 1 );
                    }
     
               }
          }
     }
}


</script>
<html>


     <body>

          <form name="frmTest">
          <input type="text" name="MonHoliday" id="MonHoliday" value="">
          <input type="text" name="TeuHoliday" id="TeuHoliday" value="">
          <input type="button" name="btnGetValue" value="Get Value" onclick="GetValue( );">
          </form>
     </body>
</html>

per a i posted earlier .. a simple modification and you get what you need
0
 
VANAuthor Commented:
FYI this has not been abandoned. I just haven't had an opportunity to test out the suggestions offered thus far. Hopefully one of you has posted what I need.
0
 
bratt030900Commented:
looks like to me you are just trying to get a member of the form object dynamically.
Consider every object javascript as an array:
object.member = object[member]
object.member.submember = object[member][submember]


<html><header><script>
function test(prefix)
{
if ( document && 
    document.forms && 
    document.forms[0] && 
    document.forms[0][prefix+'Holiday']) // make sure that the object(s) exist before using it
   return  document.forms[0][prefix+'Holiday'].value*1;
else
  return 'undefined prefix';
}
</script>
</header>
<body>
<form>
<input name="WedHoliday" value="42"/>
<input type="button" onclick="alert(test('Wed'))" />
</form>
</body>
0
 
bratt030900Commented:
object.member = object[member]
object.member.submember = object[member][submember]
should be:
object.member = object['member']
object.member.submember = object['member']['submember']
0
 
peh803Commented:
point split sounds fair...

peh803
0
 
StormyWatersCommented:
What about mine, another valid solution?
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 4
  • 3
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now