The code I have been working on incorrectly allows the form to submit when password1 and password2 do not match. I'm stumped. Any help would be greatly appreciated. I've been using this site as a reference to learn coding. The community here is outstanding. Thanks in advance.
if ($.trim(Appery("register_firstname").val()) != "") {
if ($.trim(Appery("register_lastname").val()) != "") {
if (Appery("register_password1").val() == Appery("register_password2").val()) {
if ($.trim(Appery("register_email").val()) != "") {
if (!document.getElementsByName("register_email")[0].checkValidity || document.getElementsByName("register_email")[0].checkValidity()) {
if ($.trim(Appery("register_password1").val()) != "") {
{signupService.execute({});
}
else {
document.getElementById("registrationpassword_error").innerHTML = "Please enter a password."; {
document.getElementById("register_password1").focus();
}
}
}else {
document.getElementById("registrationemail_error").innerHTML = "Email Address not valid.";
{
document.getElementById("register_email").focus();
}
}
} else {
document.getElementById("registrationemail_error").innerHTML = "Please enter your email."; {
document.getElementById("register_email").focus();
}
}
} else {
document.getElementById("registrationpassword_error").innerHTML = "Passwords do not match."; {
document.getElementById("register_password1").focus();
}
}
} else {
document.getElementById("registrationlastname_error").innerHTML = "Please enter your last name."; {
document.getElementById("register_lastname").focus();
}
//alert("Please enter your last name.");
}
} else {
document.getElementById("registrationfirstname_error").innerHTML = "Please enter your first name."; {
document.getElementById("register_firstname").focus();
}
}
}
You have too many conditional checks in series, and your missing the '}' braces to close out all of the 'if' conditions your wrote. The number of left braces and right braces should match.I would separate your 'if' conditions so that password validation is DONE before validating other inputs. Each 'if' condition should have a 'true' / 'false' output that can be checked at the end of your code to see if all conditions are 'true'. That is how I do my validation. Each conditional check is separate from the others.
I am unsure about what Appery is, but if it is a normal HTML or JSP page that you are working on then, the form will submit on click of the submit button irrespective of what the above code returns.
I assume you are using a normal button for submitting the form on click on which the above code will run and signupService.execute({}); submits the form.
Did try checking with alerts that whether the control actually goes inside the last if loop?
Related
I am trying to validate my email and Title input field but it still aloows me to submit the page. Where is the error in the code please?
if(email == "") {
printError("emailErr", "Please enter your email address");
} else {
var regex = /^\S+#\S+\.\S+$/;
if(regex.test(email) === false) {
printError("emailErr", "Please enter a valid email address");
} else{
printError("emailErr", "");
emailErr = false;
}
}
if(title == "Select") {
printError("titleErr", "Please select your title");
} else {
printError("titleErr", "");
titleErr = false;
}
if((nameErr || emailErr || titleErr) == true) {
return false;
}
I expect it to provide an error message when the submit button is clicked. Link to my entire code https://jsfiddle.net/lizzyblue02/spmygzdw/
Overall it works fine, I just had to make a couple of changes.
Your input type="submit" submits the form every time it is clicked, even if the form validation returned errors. I changed it in input type="button", and the form is now submitted using JavaScript and only if the form validation returned no error.
As mentioned in the above comment, you also needed to close the validate function later in the function.
You can see result here.
From this example
I tried to validate user input when a button was clicked.
$("#check_data").click(function () {
var $userInput = $('#user_input'); //from HTML input box id="user_input"
var pattern = " /*My user input always be like "AB1234567"*/ ";
if ($userInput.val() == '' || !pattern.test($userInput.val())) {
alert('Please enter a valid code.');
return false;
}
});
My user input input always be like "AB1234567" with this exact same characters but different 7 digits number.
I'm new to Javascript and Jquery, if you have any better way to validate user input, please suggest it.
Thank you.
You can use below regex Expression to check
/[A-Za-z0-9]/g
Your code could be like this
var _pattern=/[A-Za-z0-9]/g
if($userInput.val().match(_pattern))
{
//your code goes here..
}
Thanks
You can use the below pattern to check
/^AB\d{7}$/
You can change code to
var pattern = '/^AB\d{7}$/';
if ($userInput.val() == '' || !pattern.test($userInput.val()))
{
alert('Please enter a valid code.');
return false;
}
\d{7} matches 7 digits in the range [0-9]
You can follow below code for this:
if ($userInput.val().match(/[^a-zA-Z0-9 ]/g))
{
// it is a valid value.
} else {
// show error here
}
Hope it helps you.
Try this one.
$("#check_data").click(function(){
var $userInput = $('#user_input').val();
var pattern = /^AB[0-9]{7}?/g;
if(!$userInput.match(pattern)){
alert('Please enter a valid code.');
return false;
}
});
I have some form validation on my website.
If the account code field contains "XXX" and the reference field is blank, I want an alert to come up for the user to populate the reference field.
I have read that indexOf is the function I need, but the code below does not appear to work. Any ideas?
<SCRIPT>
if (form.account.value.indexOf("XXX") != -1 & form.reference.value == "") {
alert("Please Enter Reference Number");
form.reference.focus( );
return false;
}
</script>
Can you try:
if (form.account.value.indexOf("XXX") != -1 && form.reference.value == "") {
alert("Please Enter Reference Number");
form.reference.focus( );
return false;
}
This corrects the AND clause, which uses && not &.
Trying to load a custom form submisson error box in Facebox, when testing around with some code im having trouble getting .append() to append more than one time. If both of the example errors are failed its only loading appending and loading one in to the facebox, but if one at a time is failed it will show the correct one that is supposed to display, just not both at once..
Sorry if the code is messy / not efficient, javascript isn't my strong suit.
$('#signup').submit( function() {
var fname = $("#fname").val();
var lname = $("#lname").val();
var uname = $("#username").val();
var pass = $("#password").val();
var passc = $("#passwordc").val();
var email = $("#email").val();
if(/^[a-zA-Z0-9_]*$/.test(uname) == false) {
//alert('Usernames can be alphanumeric but may include an underscore.');
$("#errorholder").append('<tr><td style="margin: 0;">Usernames can be alphanumeric but may include a underscore.</td></tr>');
}
else if(/^[a-zA-Z']*$/.test(fname) == false) {
//alert('First name may not contain numbers or symbols.');
$("#errorholder").append('<tr><td style="margin: 0;">First name may not contain numbers or symbols</td></tr>');
}
else if(/^[a-zA-Z']*$/.test(lname) == false) {
//alert('Last name may not contain numbers or symbols.');
}
else if(!$("#fname").val()){
//alert('First name required');
}
else if(!lname){
//alert('Last name required');
}
else if(!uname){
//alert('Userame required');
}
else if(pass!=passc){
//alert('Passwords don\'t match');
}
else if(pass.length<5){
//alert('Password must be at least 5 characters.');
}
else if(!email){
//alert('Email required');
}
else if (!$("input:radio[name='gender']:checked").val()) {
// alert('Please select gender.');
}
jQuery.facebox({ div: '#piksplay' });
return false;
});
});
You're trying to test multiple conditions, but you're using else if. As soon as your code find a true condition then it doesn't test anything else, so only one append will get fired each time. These all need to be separate if statements.
I have the following validation code on my .asp webpage. I also need to validate the txtndc textbox so that the data entered looks like this. 00000-0000 Currently they can enter any info in this textbox. The entry should always be 5 numbers a dash and 4 numbers. Any help would be greatly appreciated.
function form_onsubmit() {
if (form1.txtdrug.value == "")
{
alert("Drug Name Needed");
return false;
}
if (form1.txtndc.value == "")
{
alert("NDC Number Needed");
return false;
}
if (form1.txtndc.value != "" && form1.txtdrug.value != "")
{
alert("Drug was Successfully entered into the database, hit enter to continue.");
return true;
}
}
How would the syntax be written. The textbox i am trying to check is
<input type="text" name="txtndc" size="35">.
I am not sure how to enter your code below above into my page. Please Help
Using Javascript regex#test function.
x = /^[0-9]{5}-[0-9]{4}$/;
console.log(x.test("12113-1234"));
>>> x = /^[0-9]{5}-[0-9]{4}$/; console.log(x.test("00000-0004"));
true
Demo: http://jsbin.com/axikiv/1/edit