F
F
FC182019-12-14 17:03:08
Unity
FC18, 2019-12-14 17:03:08

Restricting the use of some characters in an InputField?

How to make it so that the player can enter only one word in the InputField, and cannot use punctuation marks?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Denis Gaydak, 2019-12-14
@FC18

I don’t know, maybe it was moved somewhere with the new UIElements and renamed, but the general principle is to look at the documentation
https://docs.unity3d.com/2019.1/Documentation/Scri... with a
script to check and cut off unnecessary characters.

F
freeExec, 2019-12-14
@freeExec

Intercept changes to the input field and remove unnecessary characters from there.

L
Lev Zabudkin, 2019-12-14
@zabudkin

The simplest (codes of letters in the asci table):

<script language="JavaScript" type="text/javascript">
$(document).ready(function(){
$('.prices').keypress(function(e) {
if (!(e.which==8 || e.which==44 ||e.which==45 ||e.which==46 ||(e.which>47 && e.which<58))) return false;
});
});
</script>

Entering numbers only:
<input type="text" id="input"/>
<script>
 input = document.getElementById('input')
 input.onkeyup = function(){this.value = this.value.replace(/[^0-9\.]/g,'')}
</script>

And here is a brilliant solution (though not mine):
it makes it possible to insert one dot or one comma and numbers
<script>
function validate(inp) {
    inp.value = inp.value.replace(/[^\d,.]*/g, '')
                         .replace(/([,.])[,.]+/g, '$1')
                         .replace(/^[^\d]*(\d+([.,]\d{0,5})?).*$/g, '$1');
}

</script>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question