Jump to content

Recommended Posts

hi,

The following bit of code tries to enforce an all digits, with a maxlength of 6, input from the user.   The first alert shows the digit correctly while the second alert(check) always gives null.  

HTML:			
		.
		.
		<input type="text" class="sw_ui" name="sw_ui" value="0" maxlength="6" />
		.
		

JS:
		test = $(".test").val();	
		alert(test);
		pattern = /[0-9]{6}/g;
		check = test.match(pattern);
		alert(check);

Can someone please tell me why? What's the mistake here since I should be getting a match. 

Thanks !

Link to comment
https://forums.phpfreaks.com/topic/308950-enforce-an-all-digit-input-failing/
Share on other sites

Besides the use of .test(), note the ^ $ in Barand's pattern. Those are required to make sure the regex tests the entire value from beginning to end: without, it only tests if the value contains something matching the pattern.

  • Like 1

HI, 

Thank you all !

@daveyerwin :  While most examples use the "/ .... /g" pattern, it seems not to work.  Even I was misled by this. 😥

@Guru Barand:   Thank you, it works ! 🙏😃

@ requinix: I initially used the /^... $/ but was getting an error & since all examples I saw used the /... /g, I switched. The .test() should be .match(). If I use .test(), i get a " is not a function" error ! .match() works fine. If there is something more esoteric here that I am missing please say.

The final working code block :  

		sw_ui = $(".sw_ui").val();	
		alert(sw_ui);
		pattern = /^\d+$/;
		isnum = sw_ui.match(pattern);
		alert(isnum);

Thanks loads !!

 

 

 

3 hours ago, ajoo said:

I initially used the /^... $/ but was getting an error & since all examples I saw used the /... /g, I switched. The .test() should be .match(). If I use .test(), i get a " is not a function" error ! .match() works fine. If there is something more esoteric here that I am missing please say.

Do you know what /.../g does?

Also I didn't point out that .test() is on the RegExp, not the string. Look more carefully at his code.

And .test is the appropriate function to use. Because it tests for a match. Which is exactly what you want to do.

Hi Requinix, 

Thanks for the clarifications. Of-course you are correct. 

Quote

Do you know what /.../g does?

No I did not but I found out, it performs a global  search across the test string. 

yes so I reverted my code to exactly as pointed by Guru Barand. That was the correct code. I was getting the error because i was using it on the regex instead of the string. 

Thanks again !

 

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.