-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.

1

3 Answers 3

2
<!--[if !IE 8]-->
<script>
    ...
</script>
<!--[endif]-->
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

1 Comment

Any reason for the down vote would let me know the difference.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.