ajoo Posted July 11, 2019 Share Posted July 11, 2019 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 ! Quote Link to comment https://forums.phpfreaks.com/topic/308950-enforce-an-all-digit-input-failing/ Share on other sites More sharing options...
Barand Posted July 11, 2019 Share Posted July 11, 2019 What value is that JS supposed to be testing? I see nothing with a class = "test" Quote Link to comment https://forums.phpfreaks.com/topic/308950-enforce-an-all-digit-input-failing/#findComment-1568311 Share on other sites More sharing options...
ajoo Posted July 11, 2019 Author Share Posted July 11, 2019 Hi Guru Barand ! I am sorry the HTML is : <input type="text" class="test" name="sw_ui" value="0" maxlength="6" /> Thanks ! Quote Link to comment https://forums.phpfreaks.com/topic/308950-enforce-an-all-digit-input-failing/#findComment-1568314 Share on other sites More sharing options...
ajoo Posted July 11, 2019 Author Share Posted July 11, 2019 I mean the HTML was supposed to be the corrected one and the results it gave was a null. Kindly help to resolve. Thanks ! Quote Link to comment https://forums.phpfreaks.com/topic/308950-enforce-an-all-digit-input-failing/#findComment-1568318 Share on other sites More sharing options...
daveyerwin Posted July 11, 2019 Share Posted July 11, 2019 alert(test); pattern = new RegExp(/[0-9]{6}/g); check = test.match(pattern); alert(check); Quote Link to comment https://forums.phpfreaks.com/topic/308950-enforce-an-all-digit-input-failing/#findComment-1568321 Share on other sites More sharing options...
Barand Posted July 11, 2019 Share Posted July 11, 2019 or test = $(".sw_ui").val(); alert(test); isnum = /^\d+$/.test(test); alert(isnum); 1 Quote Link to comment https://forums.phpfreaks.com/topic/308950-enforce-an-all-digit-input-failing/#findComment-1568324 Share on other sites More sharing options...
requinix Posted July 11, 2019 Share Posted July 11, 2019 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/308950-enforce-an-all-digit-input-failing/#findComment-1568327 Share on other sites More sharing options...
ajoo Posted July 12, 2019 Author Share Posted July 12, 2019 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 !! Quote Link to comment https://forums.phpfreaks.com/topic/308950-enforce-an-all-digit-input-failing/#findComment-1568328 Share on other sites More sharing options...
requinix Posted July 12, 2019 Share Posted July 12, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/308950-enforce-an-all-digit-input-failing/#findComment-1568330 Share on other sites More sharing options...
ajoo Posted July 12, 2019 Author Share Posted July 12, 2019 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 ! Quote Link to comment https://forums.phpfreaks.com/topic/308950-enforce-an-all-digit-input-failing/#findComment-1568334 Share on other sites More sharing options...
kicken Posted July 13, 2019 Share Posted July 13, 2019 (edited) Any particular reason for using JS instead of simple HTML validation? <input type="number" min="100000" max="999999"> (Assuming 000000 is invalid) Edited July 13, 2019 by kicken Quote Link to comment https://forums.phpfreaks.com/topic/308950-enforce-an-all-digit-input-failing/#findComment-1568352 Share on other sites More sharing options...
ajoo Posted July 22, 2019 Author Share Posted July 22, 2019 Hi Kicken ! Sorry for the delayed response. In fact I just saw this. I did try this but it there were some issues which I cannot recall immediately. I'll check what the issue was and revert. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/308950-enforce-an-all-digit-input-failing/#findComment-1568588 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.