Search code examples
javascripthtmlcssjsfiddle

Unable to run jsfiddle for HTML,JS code


I am trying to validate a simple login form using HTML, CSS and javascript.

Here is my jsfiddle for the same.

For testing purpose I am trying to just give a alert message

function validate() {
  alert('testAlert');
}

But when i run the fiddle it gives the following error.

  {"error": "Shell form does not validate{'html_initial_name': u'initial-
js_lib', 'form': <mooshell.forms.ShellForm object at 0x7fb78ca3ba50>, 
'html_name': 'js_lib', 'html_initial_id': u'initial-id_js_lib', 'label': 
u'Js lib', 'field': <django.forms.models.ModelChoiceField object at 
0x7fb78c93f0d0>, 'help_text': '', 'name': 'js_lib'}{'html_initial_name': 
u'initial-js_wrap', 'form': <mooshell.forms.ShellForm object at 
0x7fb78ca3ba50>, 'html_name': 'js_wrap', 'html_initial_id': u'initial-
id_js_wrap', 'label': u'Js wrap', 'field': 
<django.forms.fields.TypedChoiceField object at 0x7fb7874c7050>, 
'help_text': '', 'name': 'js_wrap'}"}

What is the problem in my code.Any help?

Thanks in advance.


Solution

  • Two changes need to be implemented.

    First change the javascript from onLoad to body. The options are available in the javascript pane on clicking the JAVASCRIPT

    Secondly since there is a submit button, you need to prevent the default behaviour. Use ajax to submit the form

    function validate(e) { //changed here
      e.preventDefault(); // changed here
      alert('testAlert');
    }
    
    
    <form method="POST" onsubmit="return validate(event);"> // changed here
      // rest of the code 
    </form>
    

    DEMO