Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Validate Input

dday9

Super Moderator
Joined
Mar 25, 2011
Messages
9,563
I'm wanting to validate a required input by checking if:
  • There is a value
  • The value matches the pattern

I have tried this:
Code:
if($(this).val() === "" || ($(this).prop('pattern') !== "" && $(this).val().match($(this).prop('pattern'))))

And it returns True if the value is not an empty string but it also returns True regardless of if the pattern is a match or not. Any idea on how to do this?

For what it's worth, here is the fiddle that I've been working on: http://codepen.io/anon/pen/XdbaBG
I'm making a step-by-step form.
 

Ecniv

Don't Panic!
Joined
Nov 17, 2000
Messages
5,343
Shouldnt it be != not !== ?
Possibly when you dont have anything its returning null. but this might change to 'null' which isnt equal "".
Not sure if the match would then return true ...?
 

Sherin

New member
Joined
Jul 19, 2019
Messages
32
Code:
   var pattern= new RegExp("pattern");
   if(($(#id).val()) && $(#id).val().match(pattern)){
    alert("True");
   }
 
Last edited:
Top