问题
Looking for a bit of help to modify some already-working JavaScript.
I am using a piece of JavaScript along with a filtered textbox, the filtered text box only allows {1234567890,-}
The below JavaScript works and prevents the user from entering more than 1 decimal I want to also add a dash/minus {-} to the allowed characters and prevent duplicates
<script type="text/javascript">
function PreventDuplicateDecimal(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode;
var itemdecimal = evt.srcElement.value.split('.');
if (itemdecimal.length > 1 && charCode == 46)
return false;
return true;
}
</script>
I have tried adding;
I have tried adding
var itemdash = evt.srcElement.value.split('-');
and adding it as a 'or' statement but no luck
Any help would be much appreciated
Thanks in advance Paul
回答1:
if(evt.srcElement.value.indexOf(".") > -1) {
if (charCode === 46) {
return false;
}
}
if(evt.srcElement.value.indexOf("-") > -1) {
if (charCode === 45) {
return false;
}
}
return true;
来源:https://stackoverflow.com/questions/22000283/javascript-help-prevent-duplicate-characters-in-textbox