multiple names in input fields - javascript

I have a text field that posts to Mysql. It works fine.
What I want to do is have the same field do another function using javascript. It appears thats its one or the other. Not both. Any help would be nice.
This posts to MySQL (adminpassword = Mysql Field):
<input type="password" name="adminpassword" id="adminpassword" class="password" placeholder="e.g. secretpassword">
This Javascript function works, but not post to Mysql: (Obviously)
<input type="password" name="password" id="adminpassword" class="password" placeholder="e.g. secretpassword">
I have tried: (Javascript works, but does not post to MySQL)
<input type="password" name="password adminpassword" id="adminpassword" class="password" placeholder="e.g. secretpassword">
This is the Javascript:
$(document).ready(function() {
$("#showHide").click(function() {
if ($(".password").attr("type") == "password") {
$(".password").attr("type", "text");
} else {
$(".password").attr("type", "password");
}
});
});

You could use a hidden field and copy the value (before the submit) from the password input to the hidden input:
<input type="password" name="adminpassword" id="adminpassword" class="password" placeholder="e.g. secretpassword">
<input type="hidden" name="password" id="adminpassword">

Related

Disable an input element when a dependent element has changed value

<input type="password" placeholder="oldPass" id="passwordcyu" class="input-xlarge" name="passwordcyu">
<input type="password" placeholder="newPass" disabled=true class="input-xlarge" name="passwordmoi" id="passwordmoi">
<input type="password" placeholder="confirmPass" disabled=true class="input-xlarge" name="cfpw" id="cfpw" style="display:inline">
Hi everyone, I'm a new guy in javascript/jquery, I have three inputs text like above. I don't know how to disable inputs #newPass and #confirmPass when #oldPass has changed value. Please help me or give me some advises.
P/s: This is the first time I raise a question on stackoverflow. Sorry if I make someone feels uncomfortable about my question.
I'm going to assume that since your elements are already disabled in your HTML, you'll likely want to enable them. (This also fits the implied use case.)
The simplest jQuery-based answer for you would be to bind your element to a change() handler and disable the inputs as you see fit:
$("#passwordcyu").change(function() {
$("#passwordmoi, #cfpw").removeAttr("disabled");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="password" placeholder="oldPass" id="passwordcyu" class="input-xlarge" name="passwordcyu">
<input type="password" placeholder="newPass" disabled=true class="input-xlarge" name="passwordmoi" id="passwordmoi">
<input type="password" placeholder="confirmPass" disabled=true class="input-xlarge" name="cfpw" id="cfpw" style="display:inline">
Pure javascript live example:
function changeInputs() {
document.getElementById("passwordcyu").disabled = true;
document.getElementById("passwordmoi").disabled = false;
document.getElementById("cfpw").disabled = false;
}
<input type="password" placeholder="oldPass" id="passwordcyu" onchange="changeInputs()">
<input type="password" placeholder="newPass" disabled="true" id="passwordmoi">
<input type="password" placeholder="confirmPass" disabled="true" id="cfpw">
You can use prop and disabled the other input whenever old password is clicked . i.e :
//when oldpass is click
$("#passwordcyu").on("click",function(){
$("#cfpw , #passwordmoi").prop("disabled", true);
});
//when new password is click enbaled it
$('#passwordmoi ').closest("div").click(function () {
$(this).find("#cfpw,#passwordmoi").attr("disabled", false);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="password" placeholder="oldPass" id="passwordcyu" class="input-xlarge" name="passwordcyu"><br/><br/>
<div>
<input type="password" placeholder="newPass" class="input-xlarge" name="passwordmoi" id="passwordmoi"> <br/><br/>
<input type="password" placeholder="confirmPass" class="input-xlarge" name="cfpw" id="cfpw" style="display:inline">
</div>

where the error messages will be inserted when the errors that they represent occur

I'm having an issue figuring out how to write the following code:
the problem is that I'm using an empty <div> with the id error-display. This <div> is where the error messages will be inserted when the errors that they represent occur. I want to make all fields validated when the user clicks off of them to the next field. If there’s an error, an error message should be displayed. Likewise, error messages should be removed when the user fixes the problem. yet I'm not sure how to do it! here is what I got so far:
<div id="error-display">
</div>
<form id="project-form">
<br>E-mail:<span class="error">* </span>
<input type="email" name="email" id="email" placeholder="Example#example.com" required>
<br>Password:<span class="error">* </span>
<input type="password" placeholder="Password" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}" id="password" required>
<br>Confirm Password:<span class="error">* </span>
<input type="password" placeholder="Repeat Password" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}" id="password-confirmation" required>
After inserting the emipty "div id="error-display">" what can I do?
lets say the error message is: Invalid email address.
<input type="email" name="email" id="email" placeholder="Example#example.com" required>
I tried "required" in the input elements and it's working for my inputs, however, I think my professor is looking for a different solution.
I suggest you to add change event listener for every input field and to validate each of them and if some are invalid you should set innerText property of the 'error-display' div to error message text
Here's a quick and simple solution for you
in all of your input fields, put onchange="validateField(fieldID, fieldname)" like this
<input type="email" name="email" id="email" onchange="validateField('email', 'Email')" placeholder="Example#example.com" required>
then paste this script below your form (or you can have this in a separate js file)
<script>
function validateField(fieldID, fieldname) {
var inpObj = document.getElementById(fieldID);
if (!inpObj.checkValidity()) {
document.getElementById("error-display").innerHTML = "Invalid " + fieldname + ":" + inpObj.validationMessage;
} else {
//this will clear your error if the input is already correct
document.getElementById("error-display").innerHTML = "";
}
}
</script>

How do I prevent my text area (excually input type email) from not being filled

I have form. Form include name,lastname,email,phone etc. If anybody submit the form without filling email adress my database record this email adress in blank. After this situation if this situation repeat, warning message: same email adress. (First email adress =>blank Second email adress => blank) I want to use regex. Users have to fill email adress. But I could not found any examples. Thanks to much.
Please have a look at the related code:
if (userExists.equals("")) {
bindingResult.rejectValue("email", "error.user", "Lütfen email adresinizi giriniz.");
}
if (userExists != null) {
bindingResult.rejectValue("email", "error.user", "Verilen e-postayla kayıtlı bir kullanıcı var");
}
How about some JavaScript validation code perhaps something like this which doesn't let the form actually submit if the Email field is blank:
<script type="text/javascript"> <!--
function jsValidatePg() {
// Dim var.
var strValid;
// Init.
strValid = "";
// Set var.
if (document.frmMain.Email.value == "") {
strValid = "The Email field must be filled in.";
}
// Determine if valid.
if (strValid == "") {
return true;
}
else {
alert(strValid);
return false;
}
}
//-->
</script>
<form id="frmMain" name="frmMain" action="mypage2.asp" method="post">
<input type="text" name="FirstName" size="15" maxlength="25" value="">
<input type="text" name="LastName" size="15" maxlength="25" value="">
<input type="text" name="Email" size="15" maxlength="25" value="">
<input type="text" name="Phone" size="15" maxlength="25" value="">
<input type="submit" name="btnSubmit" value="Submit" onClick="return jsValidatePg();">

Populating error message with field name in Parsley js

I couldn't find anything in the Parsley docs or in Google.
Is there an easy way to set an attribute in input and populate the error message with custom message.
For example:
<label>First name
<input type="text" required/>
</label>
with give a standard error "This value is required."
But it would be nice to have something like
<label>First name
<input type="text" required data-parsley-field-name="Last name"/>
</label>
with error like "Last name is required"
Or as an option, just grad string from<label>.
I know that I can set custom messages, but you have to do it on each input.
This will your job
<form method="post" id="myForm">
<input type="text" name="phone" class="form" data-err="last name" value="" class="required" data-parsley-required="" />
<input type="text" name="phone" class="form" data-err="First name" value="" class="required" data-parsley-required="" />
<input type="submit" value="Go">
$(document).ready(function() {
$("#myForm").parsley();
$.listen('parsley:field:error', function(){
var i = 0;
$("#myForm .form").each(function(k,e){
var field = $(e).data("err");
$(e).next("ul").find("li:eq("+i+")").html(field+" is required");
});
});
});
here is working fiddle JsFiddle

Password checking in dojo

I want to check that two passwords are the same using Dojo.
Here is the HTML I have:
<form id="form" action="." dojoType="dijit.form.Form" />
<p>Password: <input type="password"
name="password1"
id="password1"
dojoType="dijit.form.ValidationTextBox"
required="true"
invalidMessage="Please type a password" /></p>
<p>Confirm: <input type="password"
name="password2"
id="password2"
dojoType="dijit.form.ValidationTextBox"
required="true"
invalidMessage="This password doesn't match your first password" /></p>
<div dojoType="dijit.form.Button" onClick="onSave">Save</div>
</form>
Here is the JavaScript I have so far:
var onSave = function() {
if(dijit.byId('form').validate()) { alert('Good form'); }
else { alert('Bad form'); }
}
Thanks for your help. I could do this in pure JavaScript, but I'm trying to find the Dojo way of doing it.
This will get you a lot closer
setting intermediateChanges=false keeps the validator running at every keystroke.
the validation dijit's constraint object is passed to its validator. Use this to pass in the other password entry
dijit.form.Form automatically calls isValid() on all its child dijits when it's submitted, and cancels submittion if they don't all validate. I though the invalid ones would get their error message, but they don't. That's left as an exercise for the reader ;-)
the validation function:
function confirmPassword(value, constraints)
{
var isValid = false;
if(constraints && constraints.other) {
var otherInput = dijit.byId(constraints.other);
if(otherInput) {
var otherValue = otherInput.value;
console.log("%s == %s ?", value, otherValue);
isValid = (value == otherValue);
}
}
return isValid;
}
function onsubmit()
{
var p1 = dijit.byId('password1').value;
var p2 = dijit.byId('password2').value;
return p1 == p2;
}
and the input objects:
<p>Password: <input type="password"
name="password1"
id="password1"
dojoType="dijit.form.ValidationTextBox"
required="true"
intermediateChanges=false
invalidMessage="Please type a password" /></p>
<p>Confirm: <input type="password"
name="password2"
id="password2"
dojoType="dijit.form.ValidationTextBox"
required="true"
constraints="{'other': 'password1'}"
validator=confirmPassword
intermediateChanges=false
invalidMessage="This password doesn't match your first password" /></p>
Even easier, use the pre-written Dojox widget, dojox.form.PasswordValidator.
http://docs.dojocampus.org/dojox/form/PasswordValidator
It does everything you want straight out of the box!
I've solved it!
This page on the Dojo forum was helpful.
I changed the HTML for the confirm password to:
<p>Confirm: <input type="password"
name="password2"
id="password2"
dojoType="dijit.form.ValidationTextBox"
required="true"
validator="return theSame(this, dijit.byId('password1'));"
invalidMessage="This password doesn't match your first password" /></p>
The only difference is the added validator parameter.
And I created the following JavaScript function:
function(dojoTxt1, dojoTxt2) {
return dojoTxt1.getValue() == dojoTxt2.getValue();
}
I think you can also use the validator parameter to create regular expressions to test against, but the documentation isn't very clear.

Categories