0

I have a web2py application and am using jQuery to validate data. I want to write Python code inside jQuery. How can I do that?

if($("#myform_parent_id")[0].value != '' 
    && $("#myform_parent_birthdate")[0].value != ''
    && 
      // Here I want to write Python code
  ..
}

Because when i make this code inside controller and then return the form it is reset selected values.

3
  • JavaScript is ran on the client-side, while Python is server-side, so you can't mix them like that. Commented Nov 1, 2009 at 12:35
  • Your example is kind of odd. The && seems to indicate you want Python within the JavaScript's if(..), but then the closing } does not match. Commented Nov 1, 2009 at 14:21
  • Also, you must not rely on jQuery (client-side scripts in general) to validate data. Always validate on the server side; add client-side validation as a service to your visitors. Failure to do so resulted recently in people ordering hundreds of pizzas for one cent each. Commented Nov 1, 2009 at 14:23

3 Answers 3

1

If you define this code in the view (instead of the controller) then you can use Python code within {{ }} blocks.

Sign up to request clarification or add additional context in comments.

Comments

1

Silverlight allows you to embed Python as a scripting language on the client-side. http://www.voidspace.org.uk/ironpython/silverlight/index.shtml

Comments

0

As no browser will understand Python, you'll have to make an Ajax call to the server if you really want to use Python. This will probably complicate things for you, if only as Ajax calls are asynchronous by definition. But jQuery does support it.

Alternatively, to keep it all within the browser, you need JavaScript-only. If you can't translate your Python to JavaScript yourself, you could take a look at automated conversions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.