I'm trying to get the name of the of the input that failed this tiny validation like so:
var username = jQuery("#modlgn-username").val();
var password = jQuery("#modlgn-passwd").val();
if(username < 2 || password < 2){
jQuery('.login-error').html('Incorrect'+jQuery(this).attr("name")).slideDown(300);
}else{
sendLogin(datasubmit);
}
But I am so far I've been very unsuccessful. Is this at all possible?
UPDATE:
Sorry my question seems to be unclear, I know how to validate the fields that parts fine.. I am struggling with:
jQuery(this).attr("name");
It does not return the failed inputs name. How would i get the failed inputs name?
As per comment
I'm just trying to check that the two fields are at least more then 2chars.
You need to use length property
if(username.length < 2 || password.length < 2){
jQuery('.login-error').html('Incorrect'+jQuery(this).attr("name")).slideDown(300);
}else{
sendLogin(datasubmit);
}
EDIT
It is very difficult to analyse this in the code fragment jQuery(this).attr("name"); without seeing complete code.
You can this, As per comment given below
var username = jQuery("#modlgn-username").val();
var password = jQuery("#modlgn-passwd").val();
if(username < 2 ){
jQuery('.login-error').html('Incorrect' + jQuery("#modlgn-username").attr("name")).slideDown(300);
}else if(password < 2){
jQuery('.login-error').html('Incorrect' + jQuery("#modlgn-passwd").attr("name")).slideDown(300);
}
else{
sendLogin(datasubmit);
}
Can you try this, Assumed you want to validate input length.
var username = jQuery("#modlgn-username").val();
var password = jQuery("#modlgn-passwd").val();
if(username.length < 2 || password.length < 2){
jQuery('.login-error').html('Incorrect'+jQuery(this).attr("name")).slideDown(300);
return false;
}else{
sendLogin(datasubmit);
}
Related
Tring to write php code to identify whether email id exists or not to prevent mail bounces. However below written code sometimes yields wrong results.
For example, I got mail bounce for michael#act-tec.com and alfiras#emirates.net.ae email ids. But when checked with below code, i get valid result for later one which is wrong and invalid result for first email id which is correct. I dont know what else check I am missing in this code. Tried many things from past 1 week but badly stuck. Please help
$isValid = 1;
$atIndex = strrpos($email, "#");
if (is_bool($atIndex) && !$atIndex)
{
$isValid = 0;
}
else
{
$domain = substr($email, $atIndex+1);
$local = substr($email, 0, $atIndex);
$localLen = strlen($local);
$domainLen = strlen($domain);
if ($localLen < 1 || $localLen > 64)
{
// local part length exceeded
$isValid = 0;
}
else if ($domainLen < 1 || $domainLen > 255)
{
// domain part length exceeded
$isValid = 0;
}
else if ($local[0] == '.' || $local[$localLen-1] == '.')
{
// local part starts or ends with '.'
$isValid = 0;
}
else if (preg_match('/\\.\\./', $local))
{
// local part has two consecutive dots
$isValid = 0;
}
else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
{
// character not valid in domain part
$isValid = 0;
}
else if (preg_match('/\\.\\./', $domain))
{
// domain part has two consecutive dots
$isValid = 0;
}
else if(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local)))
{
// character not valid in local part unless
// local part is quoted
if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\","",$local)))
{
$isValid = 0;
}
}
if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))) {
// domain not found in DNS
$isValid = 0;
}
return $isValid;
}
You can check the email syntax and check if MX (Mail exchange) servers exists, but that is pretty much it.
You cannot be 100% sure that certain email account exists in the email provider.
Even dough when sending email to some account in the major email providers, they respond that certain email account exists and allow email to be sent. Later you may receive not delivered message
Requirement : To validate password and emailID entered by user.
I have designed a dialog for user to enter there email id and password for creating their new account.
I want the the user input to be validated on the "next" button of the dialog.
I have written a JavaScript for it as shown below and added a custom action in "do action" of my dialog button.
function validatePassword(str szPasswordportal)
{
var newPassword = szPasswordportal;
var minNumberofChars = 6;
var maxNumberofChars = 20;
var regularExpression = /^[A-Za-z0-9`~!#%]{6,20}$/;
alert(newPassword);
if(newPassword = "") //if null
return false;
if(newPassword.length < minNumberofChars || newPassword.length > maxNumberofChars)
{
return false;
}
if(!regularExpression.password(newPassword))
{
alert("password should contain atleast one number ,one alphabet and one special character");
return false;
}
return true;
}
But this JS is not getting executed successfully.
Can someone help me out with this or with some other suggestion?
Your if condition have a syntax mistake.
if(newPassword = "")
= is assigning operator. If you want to check the value you have to use conditional operator == like below.
if(newPassword == "")
Also you have to add all the condition on else part, then only it will check the validation one by one, otherwise at the end it will automatically return the true value. Change your script like below.
function validatePassword(str szPasswordportal)
{
var newPassword = szPasswordportal;
var minNumberofChars = 6;
var maxNumberofChars = 20;
var regularExpression = /^[A-Za-z0-9`~!#%]{6,20}$/;
alert(newPassword);
if(newPassword == "" || newPassword.length < minNumberofChars || newPassword.length > maxNumberofChars)
{
return false;
} else if(!regularExpression.password(newPassword))
{
alert("password should contain atleast one number ,one alphabet and one special character");
return false;
} else {
return true;
}
}
I can't seem to find the answer out there for this one.
I have users filling out a form to enter their name for a ticket. This is done with one field for the whole name. Now it's important I check for both second and first names.
Everything is working perfectly. But for the life of me I can't seem to work out a new Elseif statement to check that the var contains TWO words.
Any tips?
$(document).ready(function(){
$('#user_details_form_button').live('click', function(){
var check_name_b = '';
var check_email_b = '';
var check_phone_b = '';
proceed = true;
window.location.hash = 'tickethash';
$('.userinput_name_b').each(function(){
check_name_b = $(this).val();
if(check_name_b == ''){
$(this).css('border-color','#FF0000');
$(this).attr("placeholder", "*Full Name Required");
proceed = false;
}else{
if(check_name_b.replace(/ /g,'').length < 5){
$(this).css('border-color','#FF0000');
$(this).attr("placeholder", "*Full Name Required");
$(this).val("");
proceed = false;
}else if(check_name_b.length > 40){
$(this).css('border-color','#FF0000');
$(this).attr("placeholder", "*Full Name Required");
$(this).val("");
proceed = false;
}else{
$(this).css('border-color','#CCCCCC');
}
}
});
You can use this regular expression:
if (!check_name_b.match(/^[a-z\.]+ [a-z]+/i)) {
alert('Invalid name');
}
Do not use split, like others says, because if I am just typed some spaces, then that will success also.
You can use split() to do this for you...
var check_name_b = $(this).val().trim().split(" ");
if (check_name_b.length < 2) {
// no space in name, therefore it's only one word (or none!)
}
However, I'd strongly recommend using 2 fields for first name and surname. Not only is this what people will expect, being used to seeing it on nearly all other websites, it's more robust and easier to handle input. What happens if I put "Mr Archer"? That's not my full name, but it's 2 words.
Check with this:
$(this).val().split(" ").size() > 1
Everything is working perfectly. But for the life of me I can't seem to work out a new Elseif statement to check that the var contains TWO words.
Two word mean, that there is a space between them.
Try the .split() function from jQuery.
Like:
words = input.split(' ');
if(words.length >= 2) {
//do something
}
I'm a rookie scripter and what I'm trying to do is: create a registration form and a calculator. If you enter a password less than 5 symbols or a username less than 3 symbols you will not be able to continue. But even if I enter an username with more than 3 symbols and a password with more than 5 symbols it still displays the error message.
The code: http://pastebin.com/KqYbDJMw
var user = document.forms[0].username.value;
var pw = document.forms[0].password.value;
function triggerCalc(){
if (pw.length < 5 && user.length < 3){
alert("An error occured");
}
else {
alert("Thank you for registering to my website.");
var action = prompt("Welcome to my calculator. For addition press 1, for substraction press 2, for multiplication press 3, for division press 4 and for square root press 5:", "");
var firstNum = new Array();
var secondNum = new Array();
var result = new Array();
You want to fail if either of those conditions are true, so use || (or) instead of && (and)
if (pw.length < 5 && user.length < 3)
should be
if (pw.length < 5 || user.length < 3)
Also, you want to fetch the current values each time you do your check, so this
var user = document.forms[0].username.value;
var pw = document.forms[0].password.value;
should be inside your function. Ie
function triggerCalc(){
var user = document.forms[0].username.value;
var pw = document.forms[0].password.value;
The problem is you get :
var user = document.forms[0].username.value;
var pw = document.forms[0].password.value;
function triggerCalc(){...
on load. So it will always be blank. To get it at the time the user clicks 'continue..' move it inside the function like so:
function triggerCalc(){
var user = document.forms[0].username.value;
var pw = document.forms[0].password.value;
...
You are not too far off. The two main changes are you need to put your variable assignment inside of the function you are calling so it can get the values when the submit button is pressed.
Also you probably want to use an Or instead when validating the fields. Here is an example:
function triggerCalc(){
var user = document.forms[0].username.value;
var pw = document.forms[0].password.value;
if (pw.length < 5 || user.length < 3){
alert("An error occured");
}
If you're doing any kind of validation, there are some great jQuery libraries that make it look great and are easy to implement. You might want to to a quick google search.
I'm having some issues in Qualtrics with certain functions that are easy enough in a single question, but become impossible when using a Matrix Table or a Side-by-Side.
1) Content validation for individual fields in the Matrix Table - currency in the Matrix table, you can only check validation for multiple fields and generate a single error.
2) Required Response for a field based on an entry in the previous field in that row of the Matrix Table
3) Recode Values of text entries in a Matrix Table - there doesn't seem to be a way to this in a Matrix Table. Again, it's very simple to do with a single question.
Basically, I'd like a user to be able to complete only a single row of the Matrix Table if they want, but for the rows they complete, I need to validate specific fields and require response for specific fields, and possibly re-code their text entries.
Is there an easy way to do this with Javascript instead?
Thanks...
My guess is that JavaScript will take care of it. I've had to write several scripts because the validation features in Qualtrics are limited, to say the least.
Depending on how your Qualtrics validations options are set up you'll probably have to use functions from Qualtrics' JavaScript. Below is the basic structure you would use. Not really knowing anything about your Qualtrics question ids, I used placeholders.
Update: I would recommend using the side-by-side question type.
Here's a
link to the question/survey
To get you started off, here is the code for the first student. You need to copy the same pattern for students 2-5. You can go so many ways with validation style (in-line messages, pop-ups) that I didn't even go there. The email validation you can choose in Qualtrics under content validation for the two email columns. You can also choose phone number validation as well. The validation here will just prevent a user from going to the next page until they meet all the criteria. You could explain the validation criteria in the body of the question.
The questions ids I used are listed first and correspond exactly to the placement of the items in the screen shot except for the Y/N question which is different for a side-by-side question and has the id QR~QID9#5~1~1 for yes and QR~QID9#5~1~2 for no. I don't know what your skills are regarding Firebug but you will need to find the ids that pertain to your specific questions and replace the ones below with them.
I would also recommend using Embedded Data variables to feed in the answers so that you have nice clean legible data in your download. From what I remember, the structure of Qualtrics matrix and side-by-side data are not very usable.
Hopefully, this makes sense to you. If not, ask. I know from personal experience how frustrating it is to find javascript support for Qualtrics.
Qualtrics.SurveyEngine.addOnload(function () {
/*Place Your Javascript Below This Line*/
//QR~QID9#1~1~1~TEXT - QR~QID9#2~1~1~TEXT - QR~QID9#3~1~1~TEXT - QR~QID9#4~1~1~TEXT - QR~QID9#5~1~1/QR~QID9#5~1~2 - QR~QID9#6~1~1~TEXT
//QR~QID9#1~2~1~TEXT - QR~QID9#2~2~1~TEXT - QR~QID9#3~2~1~TEXT - QR~QID9#4~2~1~TEXT - QR~QID9#5~2~1/QR~QID9#5~2~2 - QR~QID9#6~2~1~TEXT
//QR~QID9#1~3~1~TEXT - QR~QID9#2~3~1~TEXT - QR~QID9#3~3~1~TEXT - QR~QID9#4~3~1~TEXT - QR~QID9#5~3~1/QR~QID9#5~3~2 - QR~QID9#6~3~1~TEXT
//QR~QID9#1~4~1~TEXT - QR~QID9#2~4~1~TEXT - QR~QID9#3~4~1~TEXT - QR~QID9#4~4~1~TEXT - QR~QID9#5~4~1/QR~QID9#5~4~2 - QR~QID9#6~4~1~TEXT
//QR~QID9#1~5~1~TEXT - QR~QID9#2~5~1~TEXT - QR~QID9#3~5~1~TEXT - QR~QID9#4~5~1~TEXT - QR~QID9#5~5~1/QR~QID9#5~5~2 - QR~QID9#6~5~1~TEXT
var notvalidphbook1 = 0, notvalidphbook2 = 0, notvalidphbook3 = 0, notvalidphbook4 = 0, notvalidphbook5 = 0;
var phsbooks; //gets sum of notvalidphbook items
var bookrecode1, bookrecode2, bookrecode3, bookrecode4, bookrecode5;
var notvalidemail1 = 0, notvalidemail2 = 0, notvalidemail3 = 0, notvalidemail4 = 0, notvalidemail5 = 0;
var emails; //gets sum of notvalidemail items
var validates; //validation variable that decides if user proceeds or not
Qualtrics.SurveyPage.Question.prototype.validate = function (element) {
//Student name entered
var student1y = $('QR~QID9#1~1~1~TEXT').getValue();
var student2y = $('QR~QID9#1~2~1~TEXT').getValue();
var student3y = $('QR~QID9#1~3~1~TEXT').getValue();
var student4y = $('QR~QID9#1~4~1~TEXT').getValue();
var student5y = $('QR~QID9#1~5~1~TEXT').getValue();
//Student phone number
var student1phone = $('QR~QID9#2~1~1~TEXT').getValue();
var student2phone = $('QR~QID9#2~2~1~TEXT').getValue();
var student3phone = $('QR~QID9#2~3~1~TEXT').getValue();
var student4phone = $('QR~QID9#2~4~1~TEXT').getValue();
var student5phone = $('QR~QID9#2~5~1~TEXT').getValue();
//Emails match
var student1emailA = $('QR~QID9#3~1~1~TEXT').getValue();
var student1emailB = $('QR~QID9#4~1~1~TEXT').getValue();
var student2emailA = $('QR~QID9#3~2~1~TEXT').getValue();
var student2emailB = $('QR~QID9#4~2~1~TEXT').getValue();
var student3emailA = $('QR~QID9#3~3~1~TEXT').getValue();
var student3emailB = $('QR~QID9#4~3~1~TEXT').getValue();
var student4emailA = $('QR~QID9#3~4~1~TEXT').getValue();
var student4emailB = $('QR~QID9#4~4~1~TEXT').getValue();
var student5emailA = $('QR~QID9#3~5~1~TEXT').getValue();
var student6emailB = $('QR~QID9#4~5~1~TEXT').getValue();
//Student book needs
var student1booky = $('QR~QID9#5~1~1').getValue();
var student1bookn = $('QR~QID9#5~1~2').getValue();
var student2booky = $('QR~QID9#5~2~1').getValue();
var student2bookn = $('QR~QID9#5~2~2').getValue();
var student3booky = $('QR~QID9#5~3~1').getValue();
var student3bookn = $('QR~QID9#5~3~2').getValue();
var student4booky = $('QR~QID9#5~4~1').getValue();
var student4bookn = $('QR~QID9#5~4~2').getValue();
var student5booky = $('QR~QID9#5~5~1').getValue();
var student6bookn = $('QR~QID9#5~5~2').getValue();
//Student book name
var student1bookname = $('QR~QID9#6~1~1~TEXT').getValue();
var student2bookname = $('QR~QID9#6~2~1~TEXT').getValue();
var student3bookname = $('QR~QID9#6~3~1~TEXT').getValue();
var student4bookname = $('QR~QID9#6~4~1~TEXT').getValue();
var student5bookname = $('QR~QID9#6~5~1~TEXT').getValue();
if (student1y == '') {
//alert("no name provided, no other info needed");
//"no name provided, no other info needed"
} else if (student1y != '' && (student1phone == '' || (student1emailA == '' || student1emailB == '') || (student1booky == null && student1bookn == null))) {
//alert("you need to provide a phone number and enter the student's book needs");
notvalidphbook1 = 1;
}
else if (student1y != '' && (student1phone != '' && (student1emailA != '' && student1emailB != '') && (student1booky == null && student1bookn == null))) {
notvalidphbook1 = 0;
else {
//alert("thank you for providing a phone number and specifying book needs");
if (student1booky == 'QR~QID9#5~1~1') {
bookrecode1 = 'Y';
} else {
bookrecode1 = 'N';
}
}
if (student1emailA == '' && student1emailB == '') {
//alert("no email provided, no match needed");
//"no email provided, no match needed"
} else if (student1emailA != '' && student1emailA == student1emailB) {
//alert("the emails match");
//"the emails match"
notvalidemail1 = 0;
} else {
//alert("the emails don't match");
//"the emails don't match"
notvalidemail1 = 1;
}
Qualtrics.SurveyEngine.setEmbeddedData('Student1Name', student1y);
Qualtrics.SurveyEngine.setEmbeddedData('Student1Phone', student1phone);
Qualtrics.SurveyEngine.setEmbeddedData('Student1Email', student1emailA);
Qualtrics.SurveyEngine.setEmbeddedData('Student1Txt', bookrecode1);
Qualtrics.SurveyEngine.setEmbeddedData('Student1TxtName', student1bookname);
phsbooks = notvalidphbook1 + notvalidphbook2 + notvalidphbook3 + notvalidphbook4 + notvalidphbook5;
emails = notvalidemail1 + notvalidemail2 + notvalidemail3 + notvalidemail4 + notvalidemail5;
validates = phsbooks + emails;
if (validates == 0) { //validates only if the sum is equal to zero
return true; //this let's the user continue
} else {
return false; //this prevents the user from proceeding to the next page
}
}
});