• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 305
  • Last Modified:

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........
0
deakie
Asked:
deakie
  • 3
  • 2
1 Solution
 
jonathanmelnickCommented:
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)"
0
 
deakieAuthor Commented:
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)" />
0
 
deakieAuthor Commented:
Anyone out there???
0
 
jonathanmelnickCommented:
I must have been tired last night... here it is working in FF and IE7 (haven't tested others).
Using onBlur is tricky I find as it triggers the script when the user changes windows using TAB...
The only alternative I see would be to use onChange...


<SCRIPT LANGUAGE="Javascript">
<!--
var numcheck = /[^0-9]/;
function numVal(input)
{
 
      if (blurIsRunning===true) return;
      setBlurIsRunning(true);
 
      var valthis = input.value;
      if(valthis.length < 1)
      {
            alert("No text entered.");
      }
      else if(numcheck.test(valthis))
      {
            alert("Please enter numbers only!");
      }
      else {
        // if none of the above are true, then no error, return!
        setBlurIsRunning(false);
        return;
      }
     
      // we found an error !
      // wait 1 millisecond to make sure we run our code after browser has focused on new element (if user uses tab for example)
      window.setTimeout("forceFocus('"+input.id+"')",1);

}

var blurIsRunning = false;
function setBlurIsRunning(status) {
  blurIsRunning = status;
}

function forceFocus(id) {
  document.getElementById(id).focus();
  window.setTimeout("setBlurIsRunning(false)",1);
  document.getElementById(id).select();
}

//-->
</SCRIPT>


<input name="text1Value" type="text" id="text1Value" size="2" maxlength="2"  
onBlur="numVal(this)" />
<input name="text2Value" type="text" id="text2Value" size="2" maxlength="2"  
onBlur="numVal(this)" />
<input name="text3Value" type="text" id="text3Value" size="2" maxlength="2"  
onBlur="numVal(this)" />
<input name="text4Value" type="text" id="text4Value" size="2" maxlength="2"  
onBlur="numVal(this)" />
0
 
deakieAuthor Commented:
Hey!! You rock........that works perfectly!! That is exactly what I needed.  Thanks for straightening out my code.  Kudos!
Thanks, DeaKie
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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