0

I want to validate an input field input or textarea with a COMPULSIVE Alphanumeric requirement

Conditions -

  • It should compulsorily have Numbers as well as Alphabets

  • It should allow only these Special Characters , / - ( ) # . (namely * Space, Comma, Forward Slash, Hyphen, Hash, Round Brackets, Dot*)

  • It should be of minimum length (assume 10). The length here refers to the combined length of all inputs.

Please keep in mind it is compulsive validation for an address field

The input field shall be set with class name as ".address"

HTML

<input type="text" id="address_1" class="address"> <input type="text" id="address_2" class="address"> <div class="banner-message"> </div> <div class="match-message"> </div> 

jQuery/JS

$(".address").keyup(function() { address_1 = $("#address_1").val(); address_2 = $("#address_2").val(); full_address = address_1.concat(address_2); // alert(address); ADDRESS_REGEX = /^(?=.\d)(?=.[a-z])[a-z\d,.()&"##\s-]{10,}$/gi; //$(".match-message").html(ADDRESS_REGEX.test(full_address)); $(".match-message").html(full_address); // Just to check the combined value from all inputs if(ADDRESS_REGEX.test(full_address)==true) { $(".banner-message").html("Your address is perfect!"); } else { $(".banner-message").html("Please input full address"); } }); 

Check my JS Fiddle

2
  • Anything you tried to solve your problem ? if yes post that too along with question Commented Sep 14, 2019 at 1:31
  • 1
    @CodeManiac I've tried this jsfiddle.net/uvg5obzx/1 Commented Sep 14, 2019 at 1:33

2 Answers 2

3

You should use test when you want to just test whether it follows the pattern or not instead of match, test returns a boolean value whereas match returns an array of matched values.

You can use this pattern

^(?=.*\d)(?=.*[a-z])[a-z\d,.()#\s-]{10,}$ 

enter image description here

let addresses = ["hello12312","hellohello","hello-ajd-ajdnajdnasa123","hellohahahahaha-128282()#,.","a 215469874651569847"] addresses.forEach(address =>{ let isFine = /^(?=.*\d)(?=.*[a-z])[a-z\d,.()#\s-]{10,}$/i.test(address) console.log(address, isFine) })

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

3 Comments

@Alpha can elaborate which condition isn't met ?
@Alpha did you checked the fiddle i linked in answer, isn't it what you're after ?
@Alpha but it has a space and in your question you aren't allowing space, i don't see anywhere it mentioned to allow space
0

The issue was in the usage of g (global) in the regex

Correct

ADDRESS_REGEX = /^(?=.*\d)(?=.*[a-z])[a-z\d,.&"()##\s-]{30,}$/i; 

1 Comment

What is the use of ## inside character class ,second # is redundant. in question you ask for minimum length to be 10 and now you post a answer with length 30, & " weren't specified to be included allowed special characters,

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.