Categories
askquestion

jQuery Validation Plugin remote check for password with Django

jQuery Validation Plugin remote check for password with Django

Ask Question

Asked
10 years, 6 months ago

Active
8 years ago

Viewed
5k times

.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{
margin-bottom:0;
}

1

I have a “Reset Password” form being validated by the jQuery Validation plugin. Everything works fine except that I can’t get the “remote” part to work with Django. What I’m trying to do is to have remote send an ajax request to my Django backend to check that the “Old Password” is correct as part of validation.

What I’m not sure about is how to set up my url.py and views.py to return what jQuery.validation needs

Login is required, so there will be a request.user

My Code:

$(“#changePassword”).validate({
rules:
{
oldPassword:
{
required: true,
remote:
{
url: “profile/password/check/”,
type: “post”,
data:
{
oldPassword: function()
{
return $(“#oldPassword”).val();
}
}
}
},
newPassword: “required”,
confirmPassword:
{
equalTo: “#newPassword”
}
}
});

Help is much appreciated, thank you 🙂

jquery django validation

share|improve this question

asked Sep 2 ’09 at 4:41

AlexAlex

1,31222 gold badges1212 silver badges1818 bronze badges

2

You really should submit the form. I wouldn’t trust client scripts for something like this.

– ChaosPandion
Sep 2 ’09 at 4:44

Thanks ChaosPandion, I normally wouldn’t do this for passwords, but it’s safe because they need to be logged in to access this form anyway. The question still stands for anything else, like emails or names etc; how do you manage these in Django?

– Alex
Sep 2 ’09 at 6:08

add a comment
 | 

4 Answers
4

Active

Oldest

Votes

2

All you need to do is map the URL ^profile/password/check/ to a view, and within that view, fetch the value for the key oldPassword in the request.POST dictionary, do with it what you will, and return either true or false. For example:

def check_profile_password(request):
response_str = “false”
if request.is_ajax():
old_password = request.POST.get(“oldPassword”)
if old_password:
# Apply whatever logic you want to apply
response_str = “true”

return HttpResponse(“%s” % response_str)

share|improve this answer

answered Sep 2 ’09 at 8:57

ayazayaz

9,44844 gold badges2828 silver badges4848 bronze badges

This answer is somewhat helpful, but you haven’t mentioned anything about the jQuery validation API

– Alex
Sep 6 ’09 at 23:43

Alex, I wrote that code taking into account the jQuery code that you have presented in your question. I see nothing wrong in your jQuery code. My answer explains how you would set up your urls.py and views.py to work with the remote validation rule specified in the jQuery snippet.

– ayaz
Sep 7 ’09 at 6:40

add a comment
 | 

1

Okay I ended up figuring it out myself, thanks though to everyone who offered help.

From the jQuery.Validation side, I think perhaps my methods weren’t getting through or being added to the Validation modeul properly, so there was never anything wrong with Django.

I ended up simply putting “remote:” into my input tag, like so:

<input id=”oldPassword” name=”oldPassword” type=”password” remote=”/profile/password/check/” size=”25″ class=”required” minlength=”2″ />

Then to apply validation, in a tag:

$(“#changePassword”).validate();

Then in Django:

old_password = request.GET.get(“oldPassword”)
request_user = User.objects.get(id=request.user.id)
if(request_user.check_password(old_password) == True):
response_str = “true”
return HttpResponse(response_str)

share|improve this answer

answered Sep 8 ’09 at 0:24

AlexAlex

1,31222 gold badges1212 silver badges1818 bronze badges

add a comment
 | 

0

Hmm i did something like that in my django project. since im not behind the computer where i program and dont have access to my code i can only describe what i did.

I urls py i have set up some url like

/profile/save/, which calls saveProfile view. You could set up url like ^pwdvalidate/$ and call matching view.

In saveProfile view i do check

if the request.is_ajax() and if request is get. then check request.user’s existing password and do all other checks you need. Return it with simplejson dumps:
return HttpResponse(simplejson.dumps(some_python_obj))

js request do with jquery.ajax

I understand its quite vague and generic, but hope it helps.

Edit: Check out this example : http://webcloud.se/article/AJAX_in_Django_with_jQuery/

share|improve this answer

answered Sep 2 ’09 at 7:17

Odif YltsaebOdif Yltsaeb

4,6541010 gold badges4242 silver badges7373 bronze badges

add a comment
 | 

0

A word of note: remote validation DOES NOT work in older version of jQuery.
For me I could get it to work only in version 1.6.1 and above.

It will also help if you set the remote validation to execute ‘synchronously’, default being ‘asynchronous’.

remote: { url:”validate.php”, async:false }

share|improve this answer

answered Mar 3 ’12 at 7:19

Design KanyaDesign Kanya

23122 silver badges44 bronze badges

add a comment
 | 

Your Answer

Thanks for contributing an answer to Stack Overflow!Please be sure to answer the question. Provide details and share your research!But avoid …Asking for help, clarification, or responding to other answers.Making statements based on opinion; back them up with references or personal experience.To learn more, see our tips on writing great answers.

Draft saved
Draft discarded

Sign up or log in

Sign up using Google

Sign up using Facebook

Sign up using Email and Password

Submit

Post as a guest

Name

Email
Required, but never shown

Post Your Answer

Discard

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you’re looking for? Browse other questions tagged jquery django validation or ask your own question.

The Overflow Blog

Defending yourself against coronavirus scams

Podcast Episode 220: Fully Remote

Featured on Meta

Planned maintenance scheduled for Saturday, March 28, 2020 at 13:00 UTC (9AM…

An Update On Creative Commons Licensing

Community and Moderator guidelines for escalating issues via new response…

How does the Triage queue work?

Triage needs to be fixed urgently, and users need to be notified upon…

Visit chat

Linked

0

Jquery validation – checking email and username availability from server-side Django

Related

1129(Built-in) way in JavaScript to check if a string is a valid number7638How do I check if an element is hidden in jQuery?4080Setting “checked” for a checkbox with jQuery4496How do I check whether a checkbox is checked in jQuery?486Check if inputs are empty using jQuery1156Check if checkbox is checked with jQuery875How to check a radio button with jQuery?527How to check Django version0jQuery remote validate not showing an error message DJango

Hot Network Questions

Why don’t laws have examples?

Autoroute or not?

What is the point of this resistor network?

How is solid rocket fuel sourced?

Does the Buddha deny genetic differences?

What did Frodo and Sam eat when they crossed Mordor on foot?

Refactoring multiple boolean conditions?

My Braixen switched gender when it evolved

Why would a ‘first world’ alien opt to have a brood of dumb children rather than one smart one?

What are the objections to Mitch McConnell’s COVID-19 Bill?

Cook me a character meal

Replacing parts of a Matrix with lists

What is Austria?

How do “tip jet” helicopters cancel the torque effect of the main rotor?

How does FM station have fixed frequency when FM modulation changes the frequency?

How can I calculate the duration of a function call?

Do Kohanim fast on Yom Kippur in the Bet Hamikdash?

Is there a trick to help kids know which direction pawns are going?

Are Putin’s constitution amendments legal?

Is it possible to use global IP addresses without internet?

Why does camouflage make some planes fly slower?

Methods of permanently joining a metal mask to a face

Why is paragliding not an old invention?

Any Implementation in C++

more hot questions

Question feed

Subscribe to RSS

Question feed
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

default

Leave a Reply

Your email address will not be published. Required fields are marked *