I Have One text box Which should accept decimal like 12345.678 That text box should accept . before 5 decimals and after 3 decimals how to validate for this type
You can use regular expressions to match values from textarea. Required regex would be:
/^\d{5}\.{1}\d{3}$/
http://www.regexr.com/3a8rn
Is this what you are looking for?
Fiddle: Demo
HTML
<input type="text" onkeypress = "return onlyDecimal(event,this)" onblur="validateValue(this)"/>
JS
function onlyDecimal(event, elem) {
var charCode = (event.which) ? event.which : event.keyCode;
console.log(elem.value.indexOf("."));
if(elem.value.length>=9)
{
validateValue(elem);
return false;
}
if (elem.value.indexOf(".") >= 0 && elem.value.split(".").length - 1 >= 0 && charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
if ((charCode == 190 || charCode == 110 || charCode == 46) && elem.value.length>=5) //for '.'
return true;
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
}
function validateValue(elem){
if(elem.value.length<=9 && elem.value.indexOf(".") < 0)
{
alert("invalid value");
}
else if(elem.value.length<=9 && elem.value.indexOf(".") !=5)
{
alert("invalid value");
}
else if(elem.value.length>0 && elem.value.indexOf(".") >0)
{
var temp=elem.value.split(".");
if(temp[0].length<5 ||temp[1].length<3)
alert("invalid value");
}
}
Related
Currently I'm trying create a script that will only allow A-Za-z, 0-9, white space and comma. Here is my script:
<textarea name="commentText" onkeypress="return filterCharAll(event,this);"></textarea>
function filterCharAll(e, t) {
if (window.event) {
var charCode = window.event.keyCode;
}
else if (e) {
var charCode = e.which;
}
else { return true; }
if ((charCode > 47 && charCode < 58) || (charCode > 64 && charCode < 91) || (charCode > 96 && charCode < 123) || (charCode == 188 || charCode == 32 || charCode == 13)) {
return true;
} else {
return false;
}
}
Everything is working perfectly! But the comma is not working. When I press it, nothing happens
JSfiddle: https://jsfiddle.net/mek7qy8h/
Can you help me? Thank you.
You need to check the charCode of 44 to match the comma:
function filterCharAll(e, t) {
if (window.event) {
var charCode = window.event.keyCode;
} else if (e) {
var charCode = e.which;
} else {
return true;
}
if (charCode === 44 || (charCode > 47 && charCode < 58) || (charCode > 64 && charCode < 91) || (charCode > 96 && charCode < 123) || (charCode == 188 || charCode == 32 || charCode == 13)) {
return true;
} else {
return false;
}
}
<textarea name="commentText" onkeypress="return filterCharAll(event,this);"></textarea>
But it might be easier to use a regular expression and test e.key:
function filterCharAll(e) {
return /[a-z0-9\s,]/i.test(e.key);
// return true if the key is alphabetical (lower or upper),
// or digits, or whitespace, or a comma
// return false otherwise
}
<textarea name="commentText" onkeypress="return filterCharAll(event);" onpaste="return false;"></textarea>
Another option that doesn't break pasting would be to use an input listener instead, and replace all disallowed characters with the empty string:
const textarea = document.querySelector('textarea');
textarea.addEventListener('input', () => {
textarea.value = textarea.value.replace(/[^a-z0-9\s,]/gi, '');
});
<textarea></textarea>
You should not be using charCode since it is deprecated, instead you can use char and test that against a regular expression.
function filterAll(event) {
return /[A-Za-z0-9, ]/.test(event.char);
}
I've been stuck on a problem and searching for it, but couldn't find one.
I have a text field where I want to allow 10 digits and decimal values, for example my allowed digits would be:
1.11
1234567890
Anything within 10 digit range (have problems in these cases below)
12345678.13
123456789.05
1234567890.12
I can already restrict textbox to 10 digits but the question is how to allow only 2 decimals after 10 digits.
My code is:
function CheckNumber(textBoxValue,evt){
evt = (evt) ? evt : window.event;
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode == 8 || charCode == 37) {
return true;
} else if (charCode == 46 && textBoxValue.indexOf('.') != -1) {
return false;
} else if (charCode > 31 && charCode != 46 && (charCode < 48 || charCode > 57)) {
return false;
}
console.log(charCode);
if(textBoxValue.length){
newVal = textBoxValue.split('.');
console.log(newVal);
if(newVal[0].length > 9){
if(charCode == 46){
return true;
}
return false;
}
}
return true;
}
Uhhmm, why not use regex for this? Here is a working fiddle:
https://jsfiddle.net/L18d0qut/3/
EDIT:
I changed the code accordingly. Is this fiddle working for you?
I modified the checks to meet your criteria. The key code is now this:
$('#input').on('keyup', function(){
var value = $(this).val();
if(value.match(/^[0-9,.]*$/)) {
if(value.indexOf(".") >= 0 || value.indexOf(",") >= 0){
console.log("YAY, we have a float, so it is valid!");
} else if(value.length <= 10) {
console.log("Valid!");
} else {
console.log("invalid!");
}
} else {
console.log("Only numbers and . or , are allowed!");
}
});
NOTE: This works for floating numbers with . and also with ,. If you do not want this you can simply modify to your needs in the regex and indexOf() methods.
I check the length of the input AND check for the contents by regex. Is this what you're looking for?
Just have a look in the console for the result :-).
I have modified your code & created something like this.
function CheckNumber(textBoxValue,evt){
evt = (evt) ? evt : window.event;
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode == 8 || charCode == 37) {
return true;
} else if (charCode == 46 && textBoxValue.indexOf('.') != -1) {
return false;
} else if (charCode > 31 && charCode != 46 && (charCode < 48 || charCode > 57)) {
return false;
}
//console.log(charCode);
if(textBoxValue.length){
newVal = textBoxValue.split('.');
/*console.log(newVal[1]);*/
if(typeof(newVal[1])!='undefined'){
if(newVal[1].length !=2){
return true;
}else{
return false;
}
}
if(newVal[0].length > 9){
if(charCode == 46){
return true;
}
if((charCode >= 48 || charCode <=57) && newVal[0].length == 10 && textBoxValue.indexOf('.') != -1 ){
if(newVal[1].length !=2){
return true;
}else{
return false;
}
}
return false;
}
}
return true;
}
You can check
if(newVal[1].length > 2){
// your code
}
Try this..............
function CheckNumber(textBoxValue,evt){
var a = textBoxValue.replace('.', '');
if(a.length <= 10){
console.log("valid");
} else {
console.log("invalid");
}
}
Try this:
function IsValid(input) {
return (/^\d{0,10}(\.\d{0,2})?$/).test(input);
}
This allows 10 digits and 2 (optional) decimals. Input must be string.
This code allow you only two decimal digits and 10 number before decimal point
/^\d {0,12} (.\d{0,2})?$/;
you can set your textbox length by {0,12} where 0 : min length and 12 : max length
function IsValidNumber(Number) {
var expr = /^\d{0,12}(\.\d{0,2})?$/;
return expr.test(Number);
};
function ValidateNumber() {
var Number = document.getElementById("txtNumber").value;
if (!IsValidNumber(Number)) {
alert("Invalid number");
}
else {
alert("Valid number.");
}
}
<input type="text" id="txtNumber" />
<input type="button" id="btnValidate" value="Validate" onclick = "ValidateNumber()" />
Have you considered textbox input pattern using regex?
Something like pattern="\d{10}\.\d{2}$"
For my business web application I want a user to only be able to enter valid currency values in a textbox.
Currently I use
$input.val($input.val().replace(/[^.\d]/g, ''));
But this doesn't take in consideration order or multiple decimal seperators.
So the user either has to enter a whole integer or a valid decimal value e.g.:
49
49.50
Bonus points if this is allowed too:
.50 (for 0.50)
So I don't want to validate, I want to restrict typing into the textbox. Is this possible with a single regexp replace?
We can do this in two steps. Restrict the user to type only the number characters and . character.
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode < 31 && (charCode > 48 || charCode < 57))
return true;
return false;
And the next one is for allowing the .:
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode == 46 && (charCode > 31 && (charCode < 48 || charCode > 57)))
return true;
return false;
The next step will be not allowing double periods.
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode == 46 && (charCode > 31 && (charCode < 48 || charCode > 57)))
if (charCode == 46 && value.indexOf(".") !== false)
return true;
return false;
Hope you can have this as a starting point.
Snippet
Open Deal: Break it if you can.
function check(inp) {
var charCode = (event.which) ? event.which : event.keyCode;
console.log(charCode);
if (charCode == 8)
return true;
if ((charCode > 46 && charCode < 58) || (charCode > 95 && charCode < 106) || charCode == 110 || charCode == 190)
if (charCode == 110 || charCode == 190)
if (inp.value.indexOf(".") == -1)
return true;
else
return false;
else
return true;
return false;
}
<input type="text" onkeydown="return check(this);" />
It's more user friendly to advise of errors and let users fix them themselves. You might consider using the keyup event, but showing an error too early (before the user has had a chance to enter a valid value) can be annoying too. e.g.
function validate(el) {
var type = el.className;
var errEl = document.getElementById(el.name + 'Err');
if (type == 'typeCurrency') {
var currencyRe = /^-?(\d+(\.\d{1,2})?|\.\d{1,2})$/;
errEl.style.visibility = currencyRe.test(el.value)? 'hidden' : 'visible';
}
}
.errorMessage {
color: red;
background-color: white;
visibility: hidden;
}
Cost <input onblur="validate(this)" name="foo" class="typeCurrency" placeholder="0.00"><br>
<span id="fooErr" class="errorMessage">Please enter a valid cost, e.g. 2.45</span>
I am validating the text field which should accept only characters it should not allow to enter numbers it is working good but when entering unwanted text the backspace is not working. I want to work it please give me advice.
This is my code:
function Validate_Classname() {
var str = document.cclass.classname.value;
if (IsBlank(str)) {
alert("Class Name field cannot be empty")
return false;
}
if (!isNaN(str)) {
alert("Please enter only text")
return false;
}
return true;
}
function onlyAlphabets(e, t) {
try {
if (window.event) {
var charCode = window.event.keyCode;
}
else if (e) {
var charCode = e.which;
}
else {
return true;
}
if ((charCode > 64 && charCode < 91)
|| (charCode > 96 && charCode < 123))
return true;
else
return false;
}
catch (err) {
alert(err.Description);
}
}
Backspace has a keyCode of 8. Just return true when charCode == 8.
Your code should now be
if ((charCode > 64 && charCode < 91)
|| (charCode > 96 && charCode < 123) || (charCode == 8))
return true;
else
return false;
}
instead of
if ((charCode > 64 && charCode < 91)
|| (charCode > 96 && charCode < 123))
return true;
else
return false;
}
I have text boxes which only allow to enter numeric values. I handled it using a JavaScript. But it doesn't allow me to enter dot and tab movement. I need those two as well. How to change this code to enter dot symbol and tab movement.
function CheckNumeric(e) {
if (window.event) // IE
{
if ((e.keyCode < 48 || e.keyCode > 57) & e.keyCode != 8) {
event.returnValue = false;
return false;
}
}
else // Fire Fox
{
if ((e.which < 48 || e.which > 57) & e.which != 8) {
e.preventDefault();
return false;
}
}
this code is tested and works..this may help u..!!
<script type="text/javascript">
function onlyDotsAndNumbers(txt, event) {
var charCode = (event.which) ? event.which : event.keyCode
if (charCode == 46) {
if (txt.value.indexOf(".") < 0)
return true;
else
return false;
}
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
> <asp:TextBoxID="txt1"runat="server"onkeypress="return onlyDotsAndNumbers(this,event)"/>