2

This code works fine in IE but not FF. Looks like regular expression match function is not working correctly in FF. Please see the code below. Function restrictData always retuns true.

protected void Page_Load(object sender, EventArgs e)
{
    textBox.Attributes.Add("onkeyup", "chkTextBox(this)"); 
}

javascript Code**

function chkTextBox(txb) 
{
var bx = document.getElementById(txb.id);
if (restrictData(bx))
 {
     alert('You have used a special character. Please retype your answer without using special characters');
     bx.value = "";
}

}

function restrictData(cnid)
{
var inputValid = true;
 restrictSplChrRegex = "^(a-z|A-Z|0-9)*[^\]$%^&*~!?><.:\;\`/|\['\\\\\\x22]*$"; 
if (cnid.value.match(restrictSplChrRegex)) 
    inputValid =true;
else
    inputValid =false; 
return inputValid;

}

harishom
  • 33
  • 5

2 Answers2

0

Just pass only this

textBox.Attributes.Add("onkeyup", "chkTextBox(this);");

JS

function chkTextBox(textBoxID)
{
     alert('Textbox Value: ' + textBoxID.value);
}

OUTPUT

While keyup, am getting the alert box with the entered values

Example: Textbox Value: Pa$$w0rd

I have tested in IE8, FF, Chrome. Everywhere it is working fine.

Siva Charan
  • 17,940
  • 9
  • 60
  • 95
0

You should try this first:

 textBox.Attributes.Add("onKeyUp", "alert('Does it fire at all?')");

My guess is that will work. Potentially FF is case-sensitive on its attributes.

I doubt that code you posted works as you expect even in IE. Here is the best way to attach a event handler via an attribute:

 textBox.Attributes.Add("onKeyUp", "chkTextBox.apply(this, arguments)");

 function chkTextBox(ev)
 {
      alert("Text box value is: " + this.value);
 }

Note that on many browsers the ev will the event object, on IE is will be undefined since event is assigned globally.

AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
  • I can see the event firing correctly in FF and IE may be the following function needs some modification. I am calling this function to check textbox value , if user entered some special characters. function restrictData(textBox.id) { var inputValid = true; var tb = document.getElementById(textBox.id); restrictSplChrRegex = "^(a-z|A-Z|0-9)*[^\]$%^&*~!?><.:;`/|\['\\\\\\x22]*$"; if (tb.value.match(restrictSplChrRegex)) inputValid =true; else inputValid =false; return inputValid; } – harishom Feb 21 '12 at 18:42
  • Here is the complete javascript. function chkTextBox(ev) { if (!ev) ev = window.event; if (restrictData(this.id) == false) { alert('You have used a special character. Please retype your answer without using special characters'); document.getElementById(this.id).value = "";} } function restrictData(cnid) { var inputValid = true; var tb = document.getElementById(cnid); restrictSplChrRegex = "^(a-z|A-Z|0-9)*[^\]$%^&*~!?><.:;`/|\['\\\\\\x22]*$"; if (tb.value.match(restrictSplChrRegex)) inputValid =true; else inputValid =false; return inputValid; } – harishom Feb 21 '12 at 18:47
  • Hi It looks like the actual problem is the regular expression function. It actually works fine in IE but not in Firefox. It always return true.function restrictData(cnid) { var inputValid = true; restrictSplChrRegex = "^(a-z|A-Z|0-9)*[^\]$%^&*~!?><.:;`/|\['\\\\\\x22]*$"; if (cnid.value.match(restrictSplChrRegex)) inputValid =true; else inputValid =false; return inputValid; } – harishom Feb 21 '12 at 19:53
  • function restrictData(cnid) { var inputValid = true; //restrictSplChrRegex = "^(a-z|A-Z|0-9)*[^\]$%^&*~!?><.:;`/|\['\\\\\\x22]*$"; restrictSplChrRegex = "^(a-z|A-Z|0-9)*[^\]$%^&*~!?><.:;`/|\['\\\\\\x22]*$"; //restrictSplChrRegex = "/[;,\/\\$~!"; if (cnid.value.match(restrictSplChrRegex)) inputValid =true; else inputValid =false; return inputValid; } – harishom Feb 21 '12 at 19:55
  • @harishom: Your comments are unreadable, please edit your original question and include your code so that it can formatted. – AnthonyWJones Feb 21 '12 at 19:55