Link to home
Start Free TrialLog in
Avatar of deakie
deakie

asked on

string concatenation in javascript

I have a javascript that checks for numeric values in a text box and I want to be able to apply it to all textboxes on the page.....here's my code and the id that is assigend to the checkboxes on the page.....what do i need to do differently?  

<SCRIPT LANGUAGE="Javascript">
<!--
var numcheck = /[^0-9]/;
function numVal(valthis)
{

      if(valthis.length < 1)
      {
            alert("Please enter a numeric value.")
            return false;
      }
      else
            {
                  if(numcheck.test(valthis))
                  {
                        alert("Please enter numbers only!")
        for (y=1;y<=16;y++){
                        picks.text + y + Value.focus();
                        picks.text + y + Value.select();
                                    }
                        return false;
                  }
                  return true;
            }

}

//-->
</SCRIPT>

<input name="text1Value" type="text" id="text1Value" size="2" maxlength="2" onBlur="numVal this.value)" />
<input name="text2Value" type="text" id="text2Value" size="2" maxlength="2" onBlur="numVal this.value)" />
<input name="text3Value" type="text" id="text3Value" size="2" maxlength="2" onBlur="numVal this.value)" />
<input name="text4Value" type="text" id="text4Value" size="2" maxlength="2" onBlur="numVal this.value)" />

This gives the alert okay but it loses focus and does not return to the "problem" textbox.  It works fine when applied to just one textbox and the textbox id is hardcoded into the javascript.  I think I am doing something wrong with the for statement and the string concatenation........
Avatar of jonathanmelnick
jonathanmelnick

replace the 2 lines :

picks.text + y + Value.focus();
picks.text + y + Value.select();

by :

var field = document.getElementById('text'+y+'Value');
field.focus();
field.select() // though I believe this line is useless...

~jm

p.s. also be careful in your onBlur to not forget the opening parenthesis

onBlur="numVal(this.value)"
Avatar of deakie

ASKER

still doesn't work.......I get the alert but it does not focus on the field with the alpha characters in it........it just gives thte alert and goes on to the next field

here's my code with your changes:
<SCRIPT LANGUAGE="Javascript">
<!--
var numcheck = /[^0-9]/;
function numVal(valthis)
{

      if(valthis.length < 1)
      {
            alert("No text entered.")
            return false;
      }
      else
            {
                  if(numcheck.test(valthis))
                  {
                        alert("Please enter numbers only!")
                                    for (y=1;y<=16;y++){
                        var field = document.getElementById('text'+y+'Value');
                                    field.focus();
                                    }
                        return false;
                  }
                  return true;
            }

}

//-->
</SCRIPT>

<input name="text1Value" type="text" id="text1Value" size="2" maxlength="2"  
onBlur="numVal(this.value)" />
<input name="text2Value" type="text" id="text2Value" size="2" maxlength="2"  
onBlur="numVal(this.value)" />
<input name="text3Value" type="text" id="text3Value" size="2" maxlength="2"  
onBlur="numVal(this.value)" />
<input name="text4Value" type="text" id="text4Value" size="2" maxlength="2"  
onBlur="numVal(this.value)" />
Avatar of deakie

ASKER

Anyone out there???
ASKER CERTIFIED SOLUTION
Avatar of jonathanmelnick
jonathanmelnick

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of deakie

ASKER

Hey!! You rock........that works perfectly!! That is exactly what I needed.  Thanks for straightening out my code.  Kudos!
Thanks, DeaKie