-4

I have a search field validation that I do not ran if the browser is IE8.

Here's my javascript:

<script type="text/javascript">
    jQuery("#mini-search-form").submit(function() {
        var match =/[a-zA-Z0-9]+/.exec(jQuery('#mini-search-form #query').val());
      if(!match){
        alert("Please enter valid search words.");
        return false;
      }
      return true;
    });
</script>

I tried just surrounding that script with <![if !IE 8]> but Dreamweaver said that this was wrong.

Jason P
  • 26,984
  • 3
  • 31
  • 45
Pixel Reaper
  • 265
  • 5
  • 17
  • 4
    Already answered: http://stackoverflow.com/questions/6763688/disable-script-if-user-browsing-using-ie8 – Cymen Nov 04 '13 at 19:45

3 Answers3

2

Selectively running code based off of the user's browser agent isn't best practice. Having said that below is crude solution:

if(window.navigator.userAgent.indexOf('MSIE 8') == -1){
    jQuery("#mini-search-form").submit(function() {
        var match =/[a-zA-Z0-9]+/.exec(jQuery('#mini-search-form #query').val());
        if(!match){
            alert("Please enter valid search words.");
            return false;
        }
        return true;
});

Feature Detection/Progressive Enhancement is a more preferred approach to cross-browser inconsistencies.

Alex
  • 34,899
  • 5
  • 77
  • 90
2
<!--[if !IE 8]-->
<script>
    ...
</script>
<!--[endif]-->
Ruben Kazumov
  • 3,803
  • 2
  • 26
  • 39
0

You may try this but use feature detection instead of browser detection (if not only related to IE-8)

<!--[if !(IE 8)]><!-->
<script>
    jQuery(function(){
        jQuery("#mini-search-form").submit(function() {
            var match =/[a-zA-Z0-9]+/.exec(jQuery('#mini-search-form #query').val());
            if(!match){
                alert("Please enter valid search words.");
                return false;
            }
            return true;
        });
    });
</script>
<!--<![endif]-->

Read About conditional comments.

The Alpha
  • 143,660
  • 29
  • 287
  • 307