I have a asp:TextBox and I need to validate that only accept characters.
Textbox
<asp:TextBox runat="server" id="txt" onkeypress="return AllowAlphabet(event)" />
Javascript
function AllowAlphabet(e)
{
isIE = document.all ? 1 : 0
keyEntry = !isIE ? e.which : event.keyCode;
if (((keyEntry >= '65') && (keyEntry <= '90')) || ((keyEntry >= '97') && (keyEntry <= '122')) || (keyEntry == '46') || (keyEntry == '32') || keyEntry == '45')
return true;
else
{
alert('Please Enter Only Character values.');
return false;
}
}
It works good in chrome, firefox but doesn't work in IE8.
what can I do make to it work?
Wouldn't this work for you?
function AllowAlphabet(e) {
e = e || window.event;
var keyEntry = e.which || e.keyCode;
var bIsCharacter =
(keyEntry >= 65 && keyEntry <= 90) ||
(keyEntry >= 97 && keyEntry <= 122) ||
(keyEntry == 46) ||
(keyEntry == 32) ||
(keyEntry == 45);
return bIsCharacter;
}
Related
I'm trying to make the input takes only the value 99.999. I don't want to use MaxLength because it would not calculate the length of the decimal digits. I don't want to use any other functions that erase when it doesn't match a specific regex. I want it to stop it in the input.
function IsCurrencyNoMinus1 (e, thisobj, min, max) {
var keyCode = e.keyCode == 0 ? e.charCode : e.keyCode;
var ret = ((keyCode >= 48 && keyCode <= 57) || (keyCode == 44) || (keyCode == 46) || (specialKeys.indexOf(e.keyCode) != -1 && e.charCode != e.keyCode))
var inStr = $(thisobj).val();
if (ret && (keyCode == 45) && ((thisobj.selectionStart != 0) || (inStr.indexOf('-') != -1)))
ret = false;
if (ret && (keyCode == 46) && (inStr != '' && inStr.indexOf('.') != -1) && !(Math.abs(thisobj.selectionStart - thisobj.selectionEnd) == inStr.length)) {
ret = false;
}
var dotPos = (inStr.indexOf('.') != -1) ? inStr.indexOf('.') : inStr.length;
inStr = inStr.replace(/\,/g, '');
var parts = inStr.split('.');
var maxParts = max.toString().split('.');
if (ret && (inStr != '' && (keyCode >= 48 && keyCode <= 57))) {
if ((parts[0].length >= maxParts[0].length) && ((thisobj.selectionStart - thisobj.selectionEnd) == 0)
&& (thisobj.selectionStart <= dotPos)) {
ret = false;
}
if (ret && (parts[1] != undefined && parts[1].length >= 2) && ((thisobj.selectionStart - thisobj.selectionEnd) == 0)
&& (thisobj.selectionStart > dotPos) && (thisobj.selectionStart <= dotPos + 3))
ret = false;
var firstPos = thisobj.selectionStart < thisobj.selectionEnd ? thisobj.selectionStart : thisobj.selectionEnd;
if (ret && (parts[0].length >= maxParts[0].length) && (parts[1] != undefined && parts[1].length >= 1)
&& ((dotPos - firstPos == 0 && Math.abs(thisobj.selectionStart - thisobj.selectionEnd) < 4)
|| (dotPos - firstPos == 1 && (Math.abs(thisobj.selectionStart - thisobj.selectionEnd) >= 2 && Math.abs(thisobj.selectionStart - thisobj.selectionEnd) < 4))))
ret = false;
}
if (Number(inStr) > max) {
thisobj.value = '';
ret = true;
}
if (Number(inStr) < min) {
thisobj.value = '';
ret = true;
}
// var re = new RegExp(/^\(?-?[0-9]{0,12}(\.[0-9]{0,2})?\)?$/)
// if (!re.test(inStr)) {
// thisobj.value = ""
// }
return ret
}
I found the solution! Please check the code below in case someone needs it.
function Format3DigitDecimal(e, thisobj, min, max)
{
var keyCode = e.keyCode == 0 ? e.charCode : e.keyCode
var ret = ((keyCode >= 48 && keyCode <= 57) || (keyCode == 44) || (specialKeys.indexOf(e.keyCode) != -1 && e.charCode != e.keyCode))
var inStr = $(thisobj).val()
inStr = inStr.replace(/\,/g, '')
if (ret && (inStr != '' && (keyCode >= 48 && keyCode <= 57)))
{
if ((inStr.length >= max.toString().length) && ((thisobj.selectionStart - thisobj.selectionEnd) == 0))
{
ret = false
}
}
if (ret && (inStr != '' && (keyCode >= 48 && keyCode <= 57)))
{
if ((inStr.length == 2) && ((thisobj.selectionStart - thisobj.selectionEnd) == 0))
{
ret = false
}
}
return ret
}
I have a textbox that I want it to allow typing numbers from 0.00 to 24.00 only.
<asp:TextBox ID = "txtHours" runat = "Server"
onkeydown="limit(this,event);"
onkeypress="return validateFloatKeyPress(this,event);"
MaxLength="5" Width = "40" text = '<%#showData(Container.DataItem, "Hours")%>'/>
I tried using Javascript in onkeypress and onkeydown
It's still allowing typing numbers like 25,26,25.01... etc
I want it not to even allow typing 5,6,7...etc after 2 is typed.
function validateFloatKeyPress(el, evt) {
var charCode = (evt.which) ? evt.which : event.keyCode;
var number = el.value.split('.');
var numberbfr = el.value.split('.')[0];
if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
if (number.length > 1 && charCode == 46) {
return false;
}
if (numberbfr.length > 1) {
return false;
}
var caratPos = getSelectionStart(el);
var dotPos = el.value.indexOf(".");
if (caratPos > dotPos && dotPos > -1 && (number[1].length > 1)) {
return false;
}
return true;
}
function getSelectionStart(o) {
if (o.createTextRange) {
var r = document.selection.createRange().duplicate()
r.moveEnd('character', o.value.length)
if (r.text == '') return o.value.length
return o.value.lastIndexOf(r.text)
} else return o.selectionStart
}
function limit(el, evt) {
if (parseInt(el.value.charAt(0)) > 2 || (parseInt(el.value.charAt(0)) = 2 && parseInt(el.value.charAt(1)) > 4)) {
return false;
}
}
Try this.Hope this solution will help you.Copy and paste below code.'stack' is the id of the textbox.
function checkOnlyZeroTo24(evt, el) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (el.value == '' && !(charCode > 31 && (charCode < 48 || charCode > 57))) {
return true;
}
else if (el.value != '') {
if (el.value.charAt(0) < 3 && charCode == 46 && el.value.length == 1 && el.value.indexOf('.') == -1) {
document.getElementById('stack').setAttribute('maxlength', 4);
return true;
}
else if (el.value.charAt(0) < 3 && !(charCode > 31 && (charCode < 48 || charCode > 57)) && el.value.length == 1) {
document.getElementById('stack').setAttribute('maxlength', 5);
if (el.value.charAt(0) == 2 && (charCode < 48 || charCode > 52))
return false;
else
return true;
}
else if (el.value <= 24 && el.value >= 10 && charCode == 46 && el.value.indexOf('.') == -1) {
document.getElementById('stack').setAttribute('maxlength', 5);
return true;
}
else if (el.value.substr(0, el.value.indexOf('.')) <= 24 && el.value.substr(0, el.value.indexOf('.')) >= 10 && el.value.charAt(2) == '.' && !(charCode > 31 && (charCode < 48 || charCode > 57))) {
if (el.value.substr(0, el.value.indexOf('.')) == 24 && !(charCode == 48))
return false;
else
return true;
}
else if (el.value.charAt(0) < 3 && !(charCode > 31 && (charCode < 48 || charCode > 57)) && el.value.charAt(1) == '.') {
document.getElementById('stack').setAttribute('maxlength', 4);
return true;
}
else if ((el.value.charAt(0) >= 3 && charCode == 46 && el.value.indexOf('.') == -1) || (el.value.charAt(1) == '.' && !(charCode > 31 && (charCode < 48 || charCode > 57)))) {
document.getElementById('stack').setAttribute('maxlength', 4);
return true;
}
}
return false;
}
Asp:TextBox
<asp:TextBox ID = "stack" runat = "server" onkeypress="return checkOnlyZeroTo24(event,this);"> </asp:TextBox>
$("#chartModal").keypress( function (e) {
if (e.which != 8 && e.which != 0 && e.which != 43 && e.which != 45 && e.which != 46 && (e.which < 48 || e.which > 57)) {
return false;
}
});
This accept +, - & . in between the number, which is not a number.
Try with this
$(document).ready(function () {
$('#chartModal').keypress(function(e) {
var key = e.charCode || e.keyCode || 0;
var keychar = String.fromCharCode(key);
if ( ((key == 8 || key == 9 || key == 46 || key == 35 || key == 36 || (key >= 37 && key <= 40)) && e.charCode==0) /* backspace, end, begin, top, bottom, right, left, del, tab */
|| (key >= 48 && key <= 57) ) { /* 0-9 */
return;
} else {
e.preventDefault();
}
});
});
Here is an example:
$('#chartModal').keypress(function(e){
if (e.which != 8 && e.which != 0 &&e.which!= 43 && e.which!=45 && e.which !=46 &&(e.which < 48 || e.which > 57)) {
return false;
}
var charCode = e.which;
var value=$(this).val();
if(charCode==43 && value.indexOf('+')>=0) //Check whether a + is already there at beginning
return false;
if (charCode == 45 && value.indexOf('-') >= 0)//Check whether a - is already there at beginning
return false;
if (charCode == 46 && value.indexOf('.')!=-1)//Check at least one . is present in the number
return false;
});
Replace #idname with id of input.
On html I have one input box in which I just want to enter numeric values. I used following function:
function allowNumbersOnly(event,locale) {
if(locale=='en' && (event.keyCode==190 || event.keyCode==110)){
return true;
}
else if(locale=='nl' && (event.keyCode==188 || event.keyCode==190 || event.keyCode==110)){
return true;
}
if(event.keyCode==189 || (event.keyCode==109) ){
return true;
}
var key = event.charCode || event.keyCode || 0;
// allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
if (
key == 8 ||
key == 9 ||
key == 46 ||
(key >= 37 && key <= 40) ||
(key >= 48 && key <= 57) ||
(key >= 96 && key <= 105)){
}else{
event.preventDefault();
}
}
This code works fine. But when I am pressing shift key+ any number from keyboard then special character gets entered. Can anyone tell me how to solve this issue?
Try the following function
function isNumber(evt) {
evt = (evt) ? evt : window.event;
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
return true;
}
If you are using HTML5 just try
<input type="number">
Try like this:
function blockspecialcharacter(e) {
var key= document.all ? key= e.keyCode : key= e.which;
return ((key > 64 && key < 91) || (key> 96 && key< 123) || key== 8 || key== 32 || (key>= 48 && key<= 57));
}
Check demo here
shiftKey event attribute returns a Boolean value that indicates whether or not the "SHIFT" key was pressed
if (event.shiftKey==1)
{
alert("The shift key was pressed!")
}
else
{
alert("The shift key was NOT pressed!")
}
so you can exit from your function if shift is pressed using value of event.shiftKey
// in your code
function allowNumbersOnly(event,locale) {
if (event.shiftKey==1) {
return false;
} // RETURN if shift key got clicked.
if (locale=='en' && (event.keyCode==190 || event.keyCode==110)) {
return true;
}
else if (locale=='nl' && (event.keyCode == 188 ||
event.keyCode == 190 ||
event.keyCode == 110)) {
return true;
}
if (event.keyCode==189 || (event.keyCode==109)) {
return true;
}
var key = event.charCode || event.keyCode || 0;
// allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
if (key == 8 ||
key == 9 ||
key == 46 ||
(key >= 37 && key <= 40) ||
(key >= 48 && key <= 57) ||
(key >= 96 && key <= 105)) {
}
else {
event.preventDefault();
}
}
Try this:
function blockSpecialChars(e){
var e = e || window.event;
var k = e.which || e.keyCode;
var s = String.fromCharCode(k);
return !/^[\\\"\'\;\:\>\|~`!##\$%^&*\(\)]$/i.test(s);
}
Add or remove the special characters from the above code to make it as you need.
I have a problem restricting a text box to only numeric values. I used normal methods but it works only in windows, not working in ipod and it allows a user to enter strings and special characters. , if you know how to restrict this on iOS pls help ,
check it....
if ( event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 || event.keyCode == 116 ||
(event.keyCode == 65 && event.ctrlKey === true) ||
((event.keyCode >= 35 && event.keyCode <= 45) || event.keyCode == 190 || event.keyCode == 100 || event.keyCode == 110)) {
if ((event.keyCode == 190 || event.keyCode == 100 || event.keyCode == 110) && $(this).val().indexOf('.') != -1) {
event.preventDefault();
}
else {return;}
}
else {
// Ensure that it is a number and stop the keypress
if ((event.keyCode >= 219 && event.keyCode <= 222) || (event.keyCode >= 106 && event.keyCode <= 109) || event.keyCode == 111
|| event.keyCode == 32 || event.keyCode == 59 || (event.keyCode >= 186 && event.keyCode <= 189) || event.keyCode == 191 || event.keyCode == 192 ||
event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || (event.keyCode > 105 && event.keycode < 190 )) ){
/*
if (checker.apple) {
// alert('Hh');
alert(event.keyCode);
//$.browser.apple=true;
event.shiftKey || (event.keyCode <= 48 || event.keyCode >= 57) && (event.keyCode < 96 || (event.keyCode > 105 && event.keycode < 190 ))
event.preventDefault();
}*/
//alert(event.keyCode);
/*event.preventDefault();
}
}
Limit the user to only type In Numbers:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if ([string length] == 0 && range.length > 0)
{
textField.text = [textField.text stringByReplacingCharactersInRange:range withString:string];
return NO;
}
NSCharacterSet *nonNumberSet = [[NSCharacterSet characterSetWithCharactersInString:#"0123456789"] invertedSet];
if ([string stringByTrimmingCharactersInSet:nonNumberSet].length > 0)return YES;
return NO;
}
You can use onkeypress() when user presses any button and fire checkAlpha() function.
and use following code to detect whether it is a number or a alphabet
function checkAlpha() {
x = event.charCode;
if (( x >= 65 && x <=90) || (x >= 97 && x <=122))
{
alert("Yes, it is alphabet");
}
else
{
alert("Sorry, only alphabet allowed");
event.preventDefault();
}
}
works fine latest android and desktop please confirm on apple devices and let me know.
please follow this code for enter only numeric value in text box. make sure for these DELEGATE methods
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
char *x = (char*)[string UTF8String];
//if(DEBUG_MODE) NSLog(#"char index is %i",x[0]);
if([string isEqualToString:#"0"] || [string isEqualToString:#"1"] || [string isEqualToString:#"2"] || [string isEqualToString:#"3"] || [string isEqualToString:#"4"] || [string isEqualToString:#"5"] || [string isEqualToString:#"6"] || [string isEqualToString:#"7"] || [string isEqualToString:#"8"] || [string isEqualToString:#"9"] || x[0]==0 ) {
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return (newLength > 6) ? NO : YES;
} else {
return NO;
}
}