Jump to content
ajoo

enforce an all digit input failing

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 !

Share this post


Link to post
Share on other sites

What value is that JS supposed to be testing? I see nothing with a class = "test"

Share this post


Link to post
Share on other sites

Hi Guru Barand !

I am sorry the HTML is :

<input type="text" class="test" name="sw_ui" value="0" maxlength="6" />

Thanks !

 

Share this post


Link to post
Share on other sites

I mean the HTML was supposed to be the corrected one and the results it gave was a null. 

Kindly help to resolve. 

Thanks !

Share this post


Link to post
Share on other sites

alert(test);
        pattern = new RegExp(/[0-9]{6}/g);
        check = test.match(pattern);
        alert(check);

Share this post


Link to post
Share on other sites

or

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

 

  • Great Answer 1

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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 !!

 

 

 

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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 !

 

Share this post


Link to post
Share on other sites
Posted (edited)

Any particular reason for using JS instead of simple HTML validation?

<input type="number" min="100000" max="999999">

(Assuming 000000 is invalid)

Edited by kicken

Share this post


Link to post
Share on other sites

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.