You can use regular expression at client side to validate desired input against any valid format. Please see below an example of the method to validate the values in the format mentioned by you.
function IsValidCurrencyNumber(value,numberDecimalSeparator,digitsAfterDecimal)
{
//var reg = new RegExp('^\\d*[,]*[.]{0,1}\\d{1,2}$'); //For numbers upto 2 decimal
var reg = new RegExp('^\\d*[,]*['+ numberDecimalSeparator +']{0,1}\\d{1,'+digitsAfterDecimal+'}$');
// compare the argument to the RegEx
// the 'match' function returns 0 if the value didn't match
if(value.match(reg)==null)
return false
else
return true;
}
Call this method to validate your input as IsValidCurrencyNumber(500.25,'.',2). You can alter the method to hardcode the decimal seperator and allowable digits after decimal.
Hope this helps.
Please “Mark as Answer” on the reply that helps you. This will help others jump directly to a solution having similar problem.
numeric fields have a Regular Expression Validation control to make sure the input value is numeric.
asp:RegularExpressionValidator, if you enter other 0-9 , it will throw error!
<input type=text runat=server id=txtNo>
<asp:RegularExpressionValidator runat=server
ControlToValidate="txtNo"
ErrorMessage="Enter numbers alone."
ValidationExpression="^[0-9]$" />
or else use keydown event if its a windows app,
private void textBoxNumber_KeyDown(object sender, KeyEventArgs e )
{ if (Char.IsDigit(e.KeyChar) == true )
{
}
else
{
e.SuppressKeyPress = true;
}
}
or else,
write this code in ur form load event in .cs file
TextBox1.Attributes.Add("onkeypress","return CheckNemericValue(event);");//here textbox1 is ur server side control
then wite this javascript funciton in html
function CheckNemericValue(e)
{
var key;
key = e.which ? e.which : e.keyCode;
if(key>=48 && key<=57)
{
return true;
}
else
{
alert("please enter number only");
return false;
}
}
hope this will solve ur problem
just one more option,
if( document.getElementById('txtbox1') != null )
{
if( document.getElementById('txtbox1').value != "" )
{
var strValue = document.getElementById('txtbox1').value;
var objRegExp =/(^-?\d\d*$)/;
if( ! objRegExp.test(strValue) )
{
strMsg += "\n- Value must be numeric";
if( !errControlSet )
{
errControl = document.getElementById('txtbox1');
errControlSet = true;
}
result = false;
}
}
}
Vinod Bheemi...
Member
55 Points
64 Posts
Allow only one dot in a text box using javascript - client side
Feb 13, 2012 08:11 AM|LINK
Dear
All,
I need only one dot(.) in text box. Am uisng one text box for currency to store in database.
for example:
Money = 1000.50 (here need to allow to save) & one more thing need to allow "," also for sometimes
we can enter money like this also.....
Money = 10,000.00 or 100,000,000.00 (here also need to allow)
Money = 1000.50.50 (Here need to raise a javascript message like invalid money type.)
i.e., more than one dot not allow to save in the database.
This is my problem... Plz let me know about this issue have any idea & solution for it...
Advance Thanks....
Vinod Bheemisetty
dinesh kumar...
Participant
986 Points
247 Posts
Re: Allow only one dot in a text box using javascript - client side
Feb 13, 2012 08:21 AM|LINK
In the ontextchange event you can call a javascript function say "validate(this);"
and then inside the function you can check the first and last index of'.' if they are same then ok else you can show the error.
Jai Jagannath
Pankaj.Sharm...
Contributor
2350 Points
387 Posts
Re: Allow only one dot in a text box using javascript - client side
Feb 13, 2012 08:31 AM|LINK
You can use regular expression at client side to validate desired input against any valid format. Please see below an example of the method to validate the values in the format mentioned by you.
function IsValidCurrencyNumber(value,numberDecimalSeparator,digitsAfterDecimal) { //var reg = new RegExp('^\\d*[,]*[.]{0,1}\\d{1,2}$'); //For numbers upto 2 decimal var reg = new RegExp('^\\d*[,]*['+ numberDecimalSeparator +']{0,1}\\d{1,'+digitsAfterDecimal+'}$'); // compare the argument to the RegEx // the 'match' function returns 0 if the value didn't match if(value.match(reg)==null) return false else return true; }Call this method to validate your input as IsValidCurrencyNumber(500.25,'.',2). You can alter the method to hardcode the decimal seperator and allowable digits after decimal.
Hope this helps.
Vinod Bheemi...
Member
55 Points
64 Posts
Re: Allow only one dot in a text box using javascript - client side
Feb 13, 2012 08:32 AM|LINK
Thank you very much dinesh.
Me to doing same thing now. but i need java script method to handle client side, without calling text changed event.
Have to district at web page only. Have any idea let me know. thank you....
Vinod Bheemisetty
mudassarkhan
All-Star
78956 Points
13402 Posts
MVP
Re: Allow only one dot in a text box using javascript - client side
Feb 13, 2012 08:35 AM|LINK
Why don't you use MaskEditExtender
http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/MaskedEdit/MaskedEdit.aspx
Contact me
dinesh kumar...
Participant
986 Points
247 Posts
Re: Allow only one dot in a text box using javascript - client side
Feb 13, 2012 08:38 AM|LINK
Do you want to check only at submit time??
If yes then in the page_load function you can add
this.Page.Form.Attributes.Add("onsubmit","javascript:return chk()");
Jai Jagannath
srinanthuram
Contributor
6800 Points
1549 Posts
Re: Allow only one dot in a text box using javascript - client side
Feb 13, 2012 09:01 AM|LINK
hi
try it
numeric fields have a Regular Expression Validation control to make sure the input value is numeric.
asp:RegularExpressionValidator, if you enter other 0-9 , it will throw error!
<input type=text runat=server id=txtNo> <asp:RegularExpressionValidator runat=server ControlToValidate="txtNo" ErrorMessage="Enter numbers alone." ValidationExpression="^[0-9]$" /> or else use keydown event if its a windows app, private void textBoxNumber_KeyDown(object sender, KeyEventArgs e ) { if (Char.IsDigit(e.KeyChar) == true ) { } else { e.SuppressKeyPress = true; } } or else, write this code in ur form load event in .cs file TextBox1.Attributes.Add("onkeypress","return CheckNemericValue(event);");//here textbox1 is ur server side control then wite this javascript funciton in html function CheckNemericValue(e) { var key; key = e.which ? e.which : e.keyCode; if(key>=48 && key<=57) { return true; } else { alert("please enter number only"); return false; } } hope this will solve ur problem just one more option, if( document.getElementById('txtbox1') != null ) { if( document.getElementById('txtbox1').value != "" ) { var strValue = document.getElementById('txtbox1').value; var objRegExp =/(^-?\d\d*$)/; if( ! objRegExp.test(strValue) ) { strMsg += "\n- Value must be numeric"; if( !errControlSet ) { errControl = document.getElementById('txtbox1'); errControlSet = true; } result = false; } } }or
u like this url
http://www.ultrapico.com/Expresso.htm
thank u