1

I've got a mysterious problem with jQuery validation on the client side. I've got this piece of code which works in an extern js-file fine - but inline it doesn't work:

<script type="text/javascript">
$(document).delegate('#RegisterForm', 'submit', function () {
    $('#ErrorsUl').empty();

    if ($('#MyString').val() == "") {
        $('#ErrorsUl').append('<li>@MvcTest.Resources.Views.Home.Strings.MyString</li>');
        $('#Errors').show();
        return false;
    }
});
</script>

In my view I also use the standard validation of the framework. My theory is that this standard validation crashes my own validation - is that possible? When yes, how can I work around this?

My problem is, that I've to use this crap of code inline because of my resource string, which isn't accessible in extern js-file. :(

timrau
  • 22,578
  • 4
  • 51
  • 64
Robert Jaskowski
  • 823
  • 4
  • 11
  • 21
  • Do you get any error messages? – Zaphod Dec 12 '13 at 12:08
  • @Zaphod No, I get no error message :( – Robert Jaskowski Dec 12 '13 at 12:19
  • @RobertJaskowski Surely you won't be able to access MvcTest.Resources.Views.Home.Strings.MyString from an external JavaScript file? – pwdst Dec 12 '13 at 12:44
  • 2
    Have you tried placing a breakpoint in the first line of the function to see if it is being called? Assuming you are using jQuery 1.7+, I'd suggest you replace the superseded "delegate" method call (http://api.jquery.com/delegate/) with "on" (http://api.jquery.com/on/) - $(document).on('submit', '#RegisterForm', function () { ... } – pwdst Dec 12 '13 at 12:50
  • @pwdst No, I'm not sure that I can access my resources .. maybe there is a way to do this .. but I don't know how :( – Robert Jaskowski Dec 12 '13 at 13:41
  • 1
    I did get an exception .. Uncaught ReferenceError: $ is not defined .. I've overlooked it – Robert Jaskowski Dec 12 '13 at 13:43
  • 1
    [How do I debug javascript](http://stackoverflow.com/questions/988363/how-can-i-debug-my-javascript-code/19623573#19623573) – Liam Dec 12 '13 at 17:06

1 Answers1

0

First: Sorry .. I've overseen the uncaught ReferenceError: $ is not defined :(

Second: I've got the solution :)

jQuery was to late included. It was included in @section Script { ... } which seems to load on the end of the site. So I transfered my jQuery include (@Scripts.Render("~/bundles/jquery")) in the body part of my site .. and now it works fine :)

Thanks for help guys :)

Robert Jaskowski
  • 823
  • 4
  • 11
  • 21