Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specific jQuery Code Not Working in IE

I am testing a webpage I have developed, and I have several jQuery scripts running in this webpage.

The webpage displays and functions as expected in Chrome and Firefox, but I am currently testing this in Internet Explorer (version 9).

All the other jQuery scripts are working on this page, except the following script:

$(".SpecificTextboxClass").change(function() {
    if ($(this).val() == 3) {
        $(".SpecificTextboxClass").not(this).val(0);}});

UPDATE EDIT:

Here is the code for the textbox:

<input type="text" name="scale" class="Validation SpecificTextboxClass" value="1">

Could the double class be causing the problem?

SECOND UPDATE:

I just loaded the webpage in Internet Explorer 6 and had the same problem. I am also using jQuery 1.9.1 (as fas as I know).

THIRD UPDATE:

The problem seems to resolve itself when the validation code is removed:

$('.Validation').keyup(function () {
    if ("" != this.value.replace(/([0-9]|10)(\.\d{1,3})?]/g, '')) {
    this.value = this.value.replace(/[^0-9\.]/g, '').substring(0,5);}});
like image 685
Dustin Cook Avatar asked Sep 01 '26 04:09

Dustin Cook


1 Answers

Okay, thanks to your jsFiddle, I think I've found the problem.

The bug is in your validation code, in the regex pattern you're using to test whether it's valid -- you have a stray ] character at the end of the pattern.

if ("" != this.value.replace(/([0-9]|10)(\.\d{1,3})?]/g, '')) {
                                                    ^
                                              this is invalid

This is making the test fail every time, which in turn is causing it to change the value in code, which in turn appears to be stopping the change event from firing.

You can fix the problem by removing the rogue ] character.

I'd also suggest using regex .match() rather than .replace() for the test -- replace is the wrong function for an if() test like that.

You could replace it directly with this:

if (!this.value.match(/^(([0-9]|10)(\.\d{1,3})?)*$/)) {

That will have exactly the same functionality but will be a lot more efficient to run.

See here for an update to your jsFiddle to demonstrate the fix. (note that it has console.log() calls in it, so make sure you have F12 dev tools open when you run it in IE)

However even then, I'm not convinced it's excatly what you want. This will (as will your previous version) return valid for strings like 3.1346.21710.45 -- ie multiple values concatenated together. In your replace version, this is caused by the g modifier (which replaces multiple occurrences). In my version, it's done by the * at the end of the patten.

If you only want a single value, remove the g from your version, or replace the * with a ? in mine.

Finally, since it looks like you're just testing for a decimal value, you might also consider just using parseFloat() instead of all that regex.

Hope that helps.

like image 83
Spudley Avatar answered Sep 03 '26 18:09

Spudley