05
Days
23
Hours
59
Minutes
59
Seconds
  • 1

Read this first!

We do not monitor these forums. The forum is provided to exchange information and experience with other users ONLY. Forum responses are not guaranteed.

However, please submit a ticket if you have an active subscription and wish to receive support. Our ticketing system is the only way of getting in touch with RSJoomla! and receiving the official RSJoomla! Customer Support.

For more information, the Support Policy is located here.

Thank you!

TOPIC: Requiring both numbers and letters in a text field

Requiring both numbers and letters in a text field 1 month 2 weeks ago #44109

  • a2zcs
  • a2zcs's Avatar
  • OFFLINE
  • Junior Boarder
  • Posts: 36
Hi,
Trying to setup a text field that requires the submission of a "password", if you will, that requires the use of both letters and numbers. How can this be done? Can it be done?
The administrator has disabled public write access.

Requiring both numbers and letters in a text field 1 month 2 weeks ago #44110

  • dragos
  • dragos's Avatar
  • NOW ONLINE
  • RSJoomla! Official Staff
  • Posts: 665
  • Thank you received: 122
You can try using the 'Letters and numbers' validation rule, further info here.
The administrator has disabled public write access.

Requiring both numbers and letters in a text field 1 month 2 weeks ago #44111

  • a2zcs
  • a2zcs's Avatar
  • OFFLINE
  • Junior Boarder
  • Posts: 36
Hi, That seems to allow the use of either numbers or letters, but does not require them. Need to require they use BOTH numbers and letters.
The administrator has disabled public write access.

Requiring both numbers and letters in a text field 1 month 2 weeks ago #44114

  • iceferret
  • iceferret's Avatar
  • OFFLINE
  • Gold Boarder
  • Posts: 271
  • Thank you received: 70
Try this bit of javascript in the appropriate sectin of your form
document.addEventListener('DOMContentLoaded', function() {
    // ⚠️ CHANGE 'password' BELOW to match your actual RSForm field name
    // Example: if your field is called 'access_code', use: '[name="form[access_code]"]'
    var passwordField = document.querySelector('[name="form[password]"]');
 
    if (passwordField) {
        // Function to check validity and show messages
        function checkPassword() {
            var passwordValue = passwordField.value.trim();
            // Regex: at least one letter, one number, min 8 chars, only letters & numbers
            var passwordPattern = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/;
 
            // Remove previous message if any
            var oldMessage = document.querySelector('#rsform_error_password');
            if (oldMessage) oldMessage.remove();
 
            var messageSpan = document.createElement('span');
            messageSpan.id = 'rsform_error_password';
 
            if (passwordValue === '') {
                // Field empty, clear styling
                passwordField.classList.remove('valid', 'invalid');
                return false;
            } else if (!passwordPattern.test(passwordValue)) {
                // Invalid password
                messageSpan.className = 'formError';
                messageSpan.textContent = 'Password must include letters and numbers (min 8 characters).';
                passwordField.classList.remove('valid');
                passwordField.classList.add('invalid');
            } else {
                // Valid password
                messageSpan.className = 'formSuccess';
                messageSpan.textContent = 'Password looks good!';
                passwordField.classList.remove('invalid');
                passwordField.classList.add('valid');
            }
 
            // Insert message after the field
            passwordField.parentNode.insertBefore(messageSpan, passwordField.nextSibling);
        }
 
        // Events: live typing and leaving field
        passwordField.addEventListener('keyup', checkPassword);
        passwordField.addEventListener('blur', checkPassword);
 
        // Also validate on form submission
        var form = passwordField.closest('form');
        if (form) {
            form.addEventListener('submit', function(e) {
                var passwordValue = passwordField.value.trim();
                var passwordPattern = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/;
 
                if (!passwordPattern.test(passwordValue)) {
                    checkPassword(); // Show message if invalid
                    passwordField.focus();
                    e.preventDefault(); // Stop submission
                    return false;
                }
            });
        }
    }
});

Made it so that you get Instant feedback while typing and on blur. Red/green message and pevents form submission if the password is invalid.
Also you'll see a comment to show where you need to change the 'password' field name I've used to match your own RSForm field name.

Now in your 'password' field under validation use Regex and put this in the field

^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$
This sets the minimum length at 8 and requires letters and numbers
If you can keep your head when all about you are losing theirs, you obviously don't understand the situation!
Last Edit: 1 month 2 weeks ago by iceferret. Reason: added info
The administrator has disabled public write access.
The following user(s) said Thank You: gregs

Requiring both numbers and letters in a text field 1 month 2 weeks ago #44117

  • a2zcs
  • a2zcs's Avatar
  • OFFLINE
  • Junior Boarder
  • Posts: 36
Hi,
I think you are close. It does produce a message that the password is good, when both letters and numbers are used, but when submitting the form, it comes back with a "Please complete all required fields!" error and shows the password field in red indicating the password wasn't accepted.
The administrator has disabled public write access.

Requiring both numbers and letters in a text field 1 month 1 week ago #44118

  • iceferret
  • iceferret's Avatar
  • OFFLINE
  • Gold Boarder
  • Posts: 271
  • Thank you received: 70
I've tested in a couple of different forms and it works for me. Set up a simple form with just the two things, a textbox 'password' and a submit button. Does it work?
If you can keep your head when all about you are losing theirs, you obviously don't understand the situation!
The administrator has disabled public write access.

Requiring both numbers and letters in a text field 1 month 1 week ago #44119

  • a2zcs
  • a2zcs's Avatar
  • OFFLINE
  • Junior Boarder
  • Posts: 36
Hi,
I did as you suggested. Created a form with only a "password" field and a submit button. Added the script to the javascript field of the Form Properties tab and changed the Validation Rule to Regex and placed the code you gave me on the Regex Syntax field of the Textbox's Validations tab. While typing the password it tells me "Password looks good!" when I hit the 8 characters with a number. But as soon as I submit, I get "Please complete all required fields" and "Invalid Input" errors.
The administrator has disabled public write access.

Requiring both numbers and letters in a text field 1 month 1 week ago #44120

  • iceferret
  • iceferret's Avatar
  • OFFLINE
  • Gold Boarder
  • Posts: 271
  • Thank you received: 70
I see it now! My error - when i cut and pasted the regex for the validation section of the passwordfield the last / got missed off. Should be

/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/

Without the closing / you get your error.
If you can keep your head when all about you are losing theirs, you obviously don't understand the situation!
Last Edit: 1 month 1 week ago by iceferret.
The administrator has disabled public write access.

Requiring both numbers and letters in a text field 1 month 1 week ago #44121

  • a2zcs
  • a2zcs's Avatar
  • OFFLINE
  • Junior Boarder
  • Posts: 36
Hi,
I also didn't have the starting / so I tried with both the starting and ending / added and just with the ending / and no luck. I'm still getting the "Please complete all required fields" error
I've also tried with and without making the field required in the field validation tab. Same thing. However on the test form with only the password field and submit button, it did work. So what could be the issue? There isn't any other added java script on the form. There are other required fields but nothing out of the ordinary.
The administrator has disabled public write access.

Requiring both numbers and letters in a text field 1 month 1 week ago #44123

  • iceferret
  • iceferret's Avatar
  • OFFLINE
  • Gold Boarder
  • Posts: 271
  • Thank you received: 70
This has happened to me on occasions, not sure why. Now you know the password works on its own I would, assuming you have some other fields in your main form, copy those field across in small groups each time seeing if the form submits. Do this until you fi a group where it stops. One of those fields will be the problem.
If you can keep your head when all about you are losing theirs, you obviously don't understand the situation!
The administrator has disabled public write access.

Requiring both numbers and letters in a text field 1 month 1 week ago #44128

  • iceferret
  • iceferret's Avatar
  • OFFLINE
  • Gold Boarder
  • Posts: 271
  • Thank you received: 70
Here's a slightly (I hope) more robust version that also includes symbols. Message doesn't show until 8 chars have been reached. Added some comments to show what needs to change for different field name and different length.
document.addEventListener('DOMContentLoaded', function() {
    var passwordField = document.querySelector('[name="form[password]"]'); // ⚠️ change field name if needed
    if (!passwordField) return;
 
    function checkPassword(showMessage = true) {
        var value = passwordField.value.trim();
 
        // Remove any old message
        var oldMsg = document.querySelector('#rsform_error_password'); // ⚠️ change error ID if renaming
        if (oldMsg) oldMsg.remove();
 
        // Empty field is allowed
        if (value === '') {
            passwordField.classList.remove('valid', 'invalid');
            return true;
        }
 
        // ⚠️ Change this number to set a different minimum password length
        var minLength = 8;  
        var longEnough = value.length >= minLength;
 
        // Validation checks
        var hasLetter  = /[A-Za-z]/.test(value);
        var hasNumber  = /\d/.test(value);
        var hasSpecial = /[@$!%*?&]/.test(value);
        var validChars = /^[A-Za-z0-9@$!%*?&]+$/.test(value);
 
        var isValid = hasLetter && hasNumber && hasSpecial && validChars && longEnough;
 
        // Only show message if password is long enough
        if (showMessage && longEnough) {
            var msg = document.createElement('span');
            msg.id = 'rsform_error_password';
            msg.style.display = 'block';
            msg.style.marginTop = '4px';
            msg.style.fontSize = '0.9em';
            msg.style.color = isValid ? 'green' : 'red';
 
            msg.textContent = isValid
                ? 'Password looks good!'
                : `Password must include letters, numbers and a special character (@$!%*?&) and be at least ${minLength} characters.`;
 
            passwordField.parentNode.insertBefore(msg, passwordField.nextSibling);
        }
 
        // Add/remove classes for styling
        if (longEnough) {
            passwordField.classList.toggle('valid', isValid);
            passwordField.classList.toggle('invalid', !isValid);
        } else {
            passwordField.classList.remove('valid', 'invalid');
        }
 
        return isValid;
    }
 
    // Live feedback
    passwordField.addEventListener('keyup', () => checkPassword());
    passwordField.addEventListener('blur', () => checkPassword());
 
    // Validate before submission
    var form = passwordField.closest('form');
    if (form) {
        form.addEventListener('submit', function(e) {
            if (passwordField.value.trim() === '') return true;
            if (!checkPassword(false)) {
                checkPassword(true);
                passwordField.focus();
                e.preventDefault();
                return false;
            }
        }, true);
    }
});

....and in the fields validations/regex syntax
/^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/
If you can keep your head when all about you are losing theirs, you obviously don't understand the situation!
The administrator has disabled public write access.
  • 1

Read this first!

We do not monitor these forums. The forum is provided to exchange information and experience with other users ONLY. Forum responses are not guaranteed.

However, please submit a ticket if you have an active subscription and wish to receive support. Our ticketing system is the only way of getting in touch with RSJoomla! and receiving the official RSJoomla! Customer Support.

For more information, the Support Policy is located here.

Thank you!