Results 1 to 4 of 4

Thread: Validate Input

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,801

    Validate Input

    I'm wanting to validate a required input by checking if:
    1. There is a value
    2. 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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  2. #2
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: Validate Input

    Any reason why you're not using the form-validation attributes "required" and "pattern"? Alternatively, if that's not enough you could use the jQuery Validation plugin? This plugin also supports regex validation similar to the HTML5 forms pattern attribute.
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  3. #3
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Validate Input

    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 ...?

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  4. #4
    Member
    Join Date
    Jul 2019
    Location
    Ahmedabad
    Posts
    57

    Re: Validate Input

    Code:
       var pattern= new RegExp("pattern");
       if(($(#id).val()) && $(#id).val().match(pattern)){
        alert("True");
       }
    Last edited by Sherin; Oct 24th, 2019 at 01:55 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width