How to add value to if (!this.value)

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to add value to if (!this.value)



I am trying to enable my code in javascript to only allow numbers to be entered into a field. Therefore, I need the (!this.value) to equal [a-zA-Z]. How can I accomplish this?


function validatemycode()
$('datagroup').on('blur', 'input[id^="datagroup_1"]', function ()
if (!this.value)
alert('Alert message here.');

)





Using RegEx, for example
– Luca Kiebel
Aug 10 at 16:08




4 Answers
4



A Regex-free edition:


function validatemycode()
$('datagroup').on('blur', 'input[id^="datagroup_1"]', function ()
if (!isValidInput(this.value))
alert('Alert message here.');

);


function isValidInput(value)
var allowedCharacters = '0123456789'.split('');
return string.split('').every(function (char)
return allowedCharacters.includes(char);
);



What happens in isValidInput is:
1. We make an array out of 0123456789 (=['1','2','3','4','5','6','7','8','9']).


isValidInput


0123456789


['1','2','3','4','5','6','7','8','9']



We make an array out of the input (same as in the previous step).



every returns true if all of the elements of the array passes the test, which is: is the current character (of the original string) is included in the allowedCharacters array. If there's a character that is not a number, we return false.


every


true


allowedCharacters


false



This is also a nice exercise in functional programming.



Note: this won't work on Internet Explorer (since includes is not supported there).


includes





Is there a way to do this and instead of an alert message, the action of them pressing the key is prevented. (e.g. preventDefault())
– Stormy
Aug 22 at 15:48





And also, is there a regex free way to do this so it works in IE?
– Stormy
Aug 22 at 18:09





@Stormy, the problem is that you're listening to the blur event, which fires when you lose focus of the input element, so I am not sure how you can prevent from typing... You can, however, listen to the keydown/keyup event(s) and preventDefault() if the key that was just pressed is not a number. Personally, I don't like this approach as it seems confusing to the user, and it sounds to me like a not-so-good user experience.
– Catalyst
Aug 23 at 12:21


blur


keydown


keyup


preventDefault()





@Stormy, about IE support - I believe you can find a polyfill to includes. Honestly, this function is not so sophisticated by itself so you can even implement it yourself pretty easily.
– Catalyst
Aug 23 at 12:23


includes



You can use RegExp to check if there's any char that is not a number [^0-9]


RegExp


[^0-9]




$('.datagroup').on('blur', function () this.value.match(new RegExp("[^0-9]", "g")))
console.log('Not Ok');
else
console.log("Ok")

)


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="datagroup"/>





I want to know why the downvote
– Calvin Nunes
Aug 10 at 16:19






I don't know why. Looks good to me. +1
– Mark Meyer
Aug 10 at 16:20






I think it's because the asker didn't show any attempt...
– Calvin Nunes
Aug 10 at 16:23





Apologies, I needed it to work as the answer from Emeeus below has it functioning. I cannot add the additional script you included.
– Stormy
Aug 10 at 16:44





Which additional script? It's the same code you have, just added a input to use as example and the check (match()) with RegExp, that is the answer to your question
– Calvin Nunes
Aug 10 at 16:47



input


match()


RegExp



You can use a method to check whether or not the string contains something other than numbers. See below:


var containsLettersOrCharacters = function(value)

var split = value.split('');

for(var i = 0; i < split.length; i++)

if(isNaN(Number(split[i])))
return true;




return false;




Here we're breaking the string into individual characters using split(), then looping through each one and checking whether the value of Number(character) is NaN. If isNaN() returns true here, that means that the character isn't a number.


split()


Number(character)


NaN


isNaN()



The important thing here is that Number() against a string other than a number in string format will result in a NaN value.



Can use this as such:


if(containsLettersOrCharacters(value))
//the string contained something other than numbers



Hope this helps.



You could use RegExp.prototype.test() , that returns true or false.




var a = "abcABC";
var b = 1321321;
var c = "21321abc";
var d = "132 1321 ";


//only numbers
console.log(/^[0-9]+$/g.test(a))
console.log(/^[0-9]+$/g.test(b))
console.log(/^[0-9]+$/g.test(c))
console.log(/^[0-9]+$/g.test(d))



in your code:


function validatemycode()
$('datagroup').on('blur', 'input[id^="datagroup_1"]', function ()
if (/^[0-9]+$/g.test(this.value))
alert('Alert message here.');

)





How does this "only allow numbers?" What happens if there are characters that are not numbers, but also are not in the [a-zA-Z] class, such as * or %?
– Mark Meyer
Aug 10 at 16:19


[a-zA-Z]


*


%





I doesn't really answer the question then.
– Mark Meyer
Aug 10 at 16:21





Sorry, it's still broken. This doesn't test for only numbers: /[0-9]/g.test(a) it only tests that there is a number. For example, it returns true for abc123
– Mark Meyer
Aug 10 at 16:23



/[0-9]/g.test(a)


abc123





@MarkMeyer yes, you are right. My point was that .test is a good way, in my opinion, to put into an if statement because it returns true or false. I didn't take to much attention to the regex. I've updated the answer again
– Emeeus
Aug 10 at 16:47





Is there a way to also prevent the user from entering a space character as well?
– Stormy
Aug 21 at 13:50






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard