Magento - AJAX newsletter validate e-mails that already exist in the database - javascript

I'm using this ajax validation but it doesn't validate if the e-mail already exists in the database. It just goes through if you entered a valid e-mail address:
onSubmit="if(newsletterSubscriberFormDetail.validator.validate())
{
new Ajax.Updater({success:'newsletter-validate-detail'}, 'newsletter/subscriber/new',
{
asynchronous:true, evalScripts:false, onComplete:function(request, json)
{
Element.hide('newsletter-validate-detail');Element.show('pop-confirm');
},
onLoading:function(request, json){}, parameters:Form.serialize(this)
});
} return false;"
I have tried to modify the onsubmit function, but to no avail. I hope someone here can teach me how to make this validation work so that it will check if the entered e-mail already exists.

This is standard Magento behavior.
It doesn't check if the email already exists and always says "Thank you for your subscription".
You can check in Mage_Newsletter_SubscriberControllerin newAction that it'll only check for existing email if you're logged in and try to enter someone else's email address :
if ($ownerId !== null && $ownerId != $customerSession->getId()) {
Mage::throwException($this->__('This email address is already assigned to another user.'));
}
You'll probably need to rewrite this method to achieve that 'Email already exists' error.

Related

Question about web forms, JavaScript validation function unreachable

Good day,
I've been working on this for class. I submitted the assignment and got a good grade - the professor didn't even lower my grade because I wasn't able to get this working, but I'm still frustrated because what I want this to do is to take a look at the Help Request form, alert me that it's checked, and to validate the email address. But the function seems to be unreachable.
I've made this fiddle here:
https://jsfiddle.net/b7j3ekts/
Stack overflow requires that I post code as well, but the fiddle is better I think:
/* This function never fired. I spent alot of time with it and I'm really frustrated and even though I posted my problem online and am going back and forth with
it, I kinda ran out of time to fix it. I was trying to do the part of the assignment where we looked to see if email was checked and then validated the email address. */
if (document.getElementById("contactemailH").checked === true) {
alert("Checked!");
/* Checks the email field to make sure that it's an email address in formHELP */
var emailaddy2 = /^(([^<>()\[\]\\.,;:\s#"]+(\.[^<>()\[\]\\.,;:\s#"]+)*)|(".+"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if (document.forms["formHELP"]["FromAddress"].value.match(emailaddy2)) {
}
else {
alert("Please input a valid email address.");
return false;
}
}
}
The offending code starts at around line 123 of the js file.
Any advice appreciated. :)
-- Mark
if (document.getElementById("contactphoneH").checked === true ||document.getElementById("contactemailH").checked === true ) {
alert("Checked!");
/* Checks the email field to make sure that it's an email address in formHELP */
var emailaddy2 = /^(([^<>()\[\]\\.,;:\s#"]+(\.[^<>()\[\]\\.,;:\s#"]+)*)|(".+"))#((\
[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]
{2,}))$/;
if (document.forms["formHELP"]["FromAddressH"].value.match(emailaddy2)) {
}
else {
alert("Please input a valid email address.");
return false;
}
}
You just had misspelled tags.
You seem to need an event listener. The JavaScript loads once, does anything you've marked as immediate, and then it just sits there dormant. If you want a function to run you have to call it by name, or tell JavaScript to watch for an event.
In your case I'd tell JavaScript to wait for the event "blur", which is when an element was focused and then loses focus, so it's great for form fields.
document.getElementById('contactemailH').addEventListener('blur', function(event) {
validateEmail(event.target.value)
})
let validateEmail = function (emailaddy) {
// do your check here
}
Let me know if I'm off-base; I'm reading your fiddle on a mobile and jsfiddle is not mobile friendly at all.

FB.api SOMETIMES doesn't return email

I have the following method:
function CompleteFbData() {
FB.api('/me', 'get', {fields: 'id,name,email,birthday'}, function(response) {
$("#profile_full_name").val(response.name);
$("#profile_email").val(response.email);
$("#profile_telephone").focus()
$("#loginbutton").remove();
});
}
This should return me id, name, email and birthday from my user's facebook account. However, it sometimes will get me only the id and name.
Eg1:
returns email
Eg2:
doesn't return email
Since it's important that I always get the user email for my application, is there a way I can ensure it will always get the email?
Thanks for your time.
EDIT: I understand that a Facebook account doesn't necessarily have an e-mail associated to it, so I must change my application.
However, in the examples I printed, the accounts used were created by myself, both have e-mails associated to each one of the. Still it did not return the email in one of them.
Note: this happens a lot, sometimes it returns sometimes won't return. Couldn't understand why and when it happens.
Facebook api requests don't return email if the email isn't valid. Do both the emails you have used for the separate accounts have valid # addresses? also has the email been verified via facebooks verification email?
Try this and if it works..woohoo!

Suite CRM: Where the verification with database exists?

I'm using suite CRM. I need information from an expert.
Where can I find the function that verifies the existence of a field in the database before saving a form?
The scenario I have is to save a new opportunity. After choosing an account from the pop up list, I try to save the new opportunity, but I get an error:
"No match, field doesn't exist".
I found a peice of code in cache\include\javascript\sugar_grp1 :
function validate_form(formname, startsWith) {
requiredTxt = SUGAR.language.get('app_strings', 'ERR_MISSING_REQUIRED_FIELDS');
invalidTxt = SUGAR.language.get('app_strings', 'ERR_INVALID_VALUE');
if (typeof(formname) == 'undefined') {
return false;
}
I want to find where the verification with the database is done, any one could give me any information?

Invalid email when resetting password on Parse.com (JS SDK)

I am trying to reset password on my angularjs App. I am using Parse (js SDK) as a backend.
I am using Parse.User.requestPasswordReset as said in the doc, but I am always getting an error 125 invalid email address.
Here is my html form :
<input type="email" ng-model="resetData.email" required>
<button ng-click="resetPassword(resetData)">
Ok
</button>
Here is my controller :
app.controller('userCtrl', function($scope, loginService){
$scope.resetPassword = function(resetData){
loginService.resetPassword(resetData,$scope);
};
});
And finally here is my factory :
app.factory('loginService', function(){
return{
resetPassword:function(resetData,scope){
console.log(resetData.email);
Parse.User.requestPasswordReset(resetData.email,{
success:function(){
alert('You'll receive an email to reset your password');
},
error:function(error){
if (error.code === 205) {
scope.msg_erreur='User not found';
console.log("error "+error.code+" "+error.message);
}
else{
scope.msg_erreur='Oops ! Something wrong happened';
console.log("error "+error.code+" "+error.message);
};
}
})
}
}
});
Good to know :
the console.log(resetData.email) show the good email address.
the email address I used are really in the User Class (if not there is an "error 205 user not found")
I tried to put an email address directly in my code in the Parse.User.requestPasswordReset instead of resetData.email
I tried with several email address, all are real ones.
In every case I always have this error 125 invalid email address.
Does anyone have an idea ?
Thanks
I presume you are storing the email address in the username field, and letting users login that way. Put the email address in the email field as well and the reset email should go out as expected.
This seems to be an issue with the way Parse is dealing with resets, because this was not a problem before. This workaround is not elegant, but it will work until the issue is properly resolved from their end.

Check if correct e-mail was entered

I have a field for users to write their e-mail address in. It is optional, so I need to first check if something was entered in the field, if nothing was entered then do nothing, but if something was entered than check if it is an e-mail.
Right now I'm at this point
var email = $("input#sc_email").val();
if (email == "") {
// If e-mail field is empty do nothing
} else {
// If something was entered
// [CODE HERE] Check if valid e-mail was entered, if not show error message
$("label#email_error").show(); //error message
$("input#sc_email").focus(); //focus on email field
return false;
}
I'm not even sure if that is right, but I think it is. Right now I need help with gaps in code, I marked them as [CODE HERE]. Could anyone suggest a solution please.
You could use the jQuery validate plugin for this, but if you only want to check the validity of an email address in one field, that is a little bit of overkill.
Instead, the regular expression in the function below will make sure the format of the email address is correct, should a value have been entered.
var email = $("input#sc_email").val();
if (email !== "") { // If something was entered
if (!isValidEmailAddress(email)) {
$("label#email_error").show(); //error message
$("input#sc_email").focus(); //focus on email field
return false;
}
}
function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^(("[\w-+\s]+")|([\w-+]+(?:\.[\w-+]+)*)|("[\w-+\s]+")([\w-+]+(?:\.[\w-+]+)*))(#((?:[\w-+]+\.)*\w[\w-+]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(#\[?((25[0-5]\.|2[0-4][\d]\.|1[\d]{2}\.|[\d]{1,2}\.))((25[0-5]|2[0-4][\d]|1[\d]{2}|[\d]{1,2})\.){2}(25[0-5]|2[0-4][\d]|1[\d]{2}|[\d]{1,2})\]?$)/i);
return pattern.test(emailAddress);
};
You could use regular expressions to validate the email address in JavaScript.
function validateEmail(email) {
var re = /^(([^<>()[\]\\.,;:\s#\"]+(\.[^<>()[\]\\.,;:\s#\"]+)*)|(\
".+\"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA
-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
This will, however, only validate that it is a valid email address formatted string. flibble#flobble.com would be valid, even if no-one actually has that email address.
Have a look at this question.
I would recommend validation both at client side and at server side, you could simply use this code:
alert(/\S+#\S+\.\S+/.test('asdasd#asdasd#asdd.com'));
alert(/\S+#\S+\.\S+/.test('asdasd234#asdd.com'));
but importantly, use server side validation and methodology, like sending click-link-mail, to verify your client email address or mailing random number etc.

Categories

Resources