0

I am trying to figure out how to get my jquery validation working so that if a value is less than the desired amount it will display the error code. I can't figure out how to make this work correctly. It requires at least one character to be input but I want it to be at least 4.

Any help appreciated

My validation code looks like

   if($('#firstname').val() < 3){
                $('#firstname').parent().parent().find('.form-error').html("First Name is Required");
                err++;
            }

3 Answers 3

1

You are comparing the actual value of the string instead of the length like I assume you want to. just add a .length to your condition. Like so:

if($('#firstname').val().length < 4)

Sign up to request clarification or add additional context in comments.

Comments

0
if($('#firstname').val().length < 4){
    ...

Comments

0

The .length may simply fix your problem.
But I suggest using regex to check the input, otherwise user can just input 4 space which will also past the .length check.

str = "    " # 4 space
str.length >= 4 # => true
str.match(/^\w{4}.*$/) # => null
another_str = "someinput"
another_str.match(/^\w{4}.*$/) # => ["someinput"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.