Solved

Wildcard form input names? Simple.

Posted on 2004-08-18
22
727 Views
Last Modified: 2010-07-27
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
Comment
Question by:VAN
  • 6
  • 4
  • 3
  • +4
22 Comments
 
LVL 19

Expert Comment

by:peh803
ID: 11834596
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
 
LVL 1

Author Comment

by:VAN
ID: 11834616
Mon, Tue, Wed, Thu, Fri, Sat, Sun

Those would be acceptable.
0
 
LVL 19

Expert Comment

by:peh803
ID: 11834629
<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
 
LVL 15

Expert Comment

by:justinbillig
ID: 11834704
<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
 
LVL 1

Author Comment

by:VAN
ID: 11834707
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
 
LVL 15

Expert Comment

by:justinbillig
ID: 11834708
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
 
LVL 15

Expert Comment

by:Colosseo
ID: 11834741
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
 
LVL 13

Assisted Solution

by:StormyWaters
StormyWaters earned 100 total points
ID: 11834773
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
 
LVL 19

Expert Comment

by:peh803
ID: 11834805
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 19

Expert Comment

by:peh803
ID: 11834818
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
 
LVL 1

Author Comment

by:VAN
ID: 11834955
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
 
LVL 19

Accepted Solution

by:
peh803 earned 100 total points
ID: 11835024
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
 
LVL 19

Assisted Solution

by:Dexstar
Dexstar earned 100 total points
ID: 11835483
@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
 
LVL 15

Assisted Solution

by:justinbillig
justinbillig earned 100 total points
ID: 11845011
<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
 
LVL 1

Author Comment

by:VAN
ID: 11852054
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
 
LVL 2

Assisted Solution

by:bratt030900
bratt030900 earned 100 total points
ID: 11897142
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
 
LVL 2

Expert Comment

by:bratt030900
ID: 11897161
object.member = object[member]
object.member.submember = object[member][submember]
should be:
object.member = object['member']
object.member.submember = object['member']['submember']
0
 
LVL 19

Expert Comment

by:peh803
ID: 12300965
point split sounds fair...

peh803
0
 
LVL 13

Expert Comment

by:StormyWaters
ID: 12385844
What about mine, another valid solution?
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now