In the below code i have a textbox my textbox should return a date format as yyyy-mm-dd.But it returns dd-mm-yyyy.Pls help me to rectify the issue.
js:
function Reportdate(txt, keyCode) {
alert(txt);
if (keyCode == 8) { txt.value = substr(0, txt.value.length - 1); return; }
var dt = txt.value;
var da = dt.split('-');
for (var a = 0; a < da.length; a++) { if (da[a] != +da[a]) da[a] = da[a].substr(0, da[a].length - 1); }
if (da[0] > 9999) da[1] = da[0].substr(0, da[0].length - 1);
if (da[1] > 12) { da[2] = da[1].substr(da[1].length - 1, 1); da[1] = '0' + da[1].substr(0, da[1].length - 1); }
if (da[2] > 31) { da[1] = da[2].substr(da[2].length - 1, 1); da[2] = '0' + da[2].substr(0, da[2].length - 1); }
dt = da.join('-');
if (dt.length == 2 || dt.length == 5) dt += '-';
txt.value = dt;
}
ASP.net
<asp:TextBox ID="txtFromDate" onkeydown = "return Reportdate(this, event.keyCode)" runat="server"></asp:TextBox>
Related
this is my code someone can help me i cant use if else on get current date i try all google tutorial no one work for me please good help for me if someone help me thank you in advance the only problem if else if i run this code if else wont read it
<script type="text/javascript">
function calc() {
var today = new Date();
var month = today.getMonth(); // Returns 9
console.log(month); // Output: 9
var textValue3 = document.getElementById('input3').value;
var textValue2 = document.getElementById('input2').value
var textValue1 = document.getElementById('input1').value;
var basic = 5;
if (month = '1') {
var rate_interest = 0;
}
else if (month = '2') {
var rate_interest = 0;
}
else if (month = '3') {
var rate_interest = 0.06;
}
else if (month = '4') {
var rate_interest = 0.08;
}
else if (month = '5') {
var rate_interest = 0.10;
}
else if (month = '6') {
var rate_interest = 0.12;
}
else if (month = '7') {
var rate_interest = 0.14;
}
else if (month = '8') {
var rate_interest = 0.16;
}
else if (month = '9') {
var rate_interest = 0.18;
}
else if (month = '10') {
var rate_interest = 0.20;
}
else if (month = '11') {
var rate_interest = 0.22;
}
else if (month = '12') {
var rate_interest = 0.24;
}
document.getElementById('output').value = (basic) + (textValue1 / 1000) + (textValue2 / 1000) + (textValue3 / 1000) + (basic * rate_interest);
}
</script>
In the 'if condition' you need to write == instead of =
date.getMonth() return the month in 0 to 11 so you need to plus one in a month.
function calc() {
var today = new Date();
var month = today.getMonth(); // Returns 9
month = month + 1;
console.log(month); // Output: 9
var textValue3 = document.getElementById('input3').value;
var textValue2 = document.getElementById('input2').value
var textValue1 = document.getElementById('input1').value;
var basic = 5;
var rate_interest;
if (month == 1) {
rate_interest = 0;
}
else if (month == 2) {
rate_interest = 0;
}
else if (month == 3) {
rate_interest = 0.06;
}
else if (month == 4) {
rate_interest = 0.08;
}
else if (month == 5) {
rate_interest = 0.10;
}
else if (month == 6) {
rate_interest = 0.12;
}
else if (month == 7) {
rate_interest = 0.14;
}
else if (month == 8) {
rate_interest = 0.16;
}
else if (month == 9) {
rate_interest = 0.18;
}
else if (month == 10) {
rate_interest = 0.20;
}
else if (month == 11) {
rate_interest = 0.22;
}
else if (month == 12) {
rate_interest = 0.24;
}
document.getElementById('output').value = (basic) + (textValue1 / 1000) + (textValue2 / 1000) + (textValue3 / 1000) + (basic * rate_interest);
}
I am trying to make the input date of birth mask in angular and format of date is dd/mm/yyyy ,but it not set and return the input according to our requirement input value.
my code given below.
<input type="text" placeholder="{{timePlaceholder}}" (focus)="showlable()" (focusout)="hidelable()" (keypress)="this.value =fixDatePattern($event);">
currentDate:any = "";
currentLength:any ="";
lastNumberEntered:any ="";
transformedDate:any="";
dateCountTracker:any="";
fixDatePattern(event) {
this.currentDate = event.target.value;
this.currentLength = this.currentDate.length;
this.lastNumberEntered = this.currentDate[this.currentLength - 1];
if (this.currentLength > 10) {
return this.currentDate.substring(0, 10);
}
if (this.currentLength == 1 && this.currentDate > 1) {
this.transformedDate = "0" + this.currentDate + '/';
this.dateCountTracker = 2;
this.currentLength = this.transformedDate.length;
return this.transformedDate;
} else if (this.currentLength == 4 && this.currentDate[3] > 3) {
this.transformedDate = this.currentDate.substring(0, 3) + "0" + this.currentDate[3] + '/';
this.dateCountTracker = 5;
this.currentLength = this.transformedDate.length;
return this.transformedDate;
} else if (this.currentLength == 2 && (this.dateCountTracker != 2 && this.dateCountTracker != 3)) {
this.dateCountTracker = this.currentLength;
return this.currentDate + '/';
} else if (this.currentLength == 5 && (this.dateCountTracker != 5 && this.dateCountTracker != 6)) {
this.dateCountTracker = this.currentLength;
return this.currentDate + '/';
}
this.dateCountTracker = this.currentLength;
return this.currentDate;
}
<input placeholder="mm/dd/yyyy" (input)="KeyUpCalled($event.target.value)" maxlength="10" [(ngModel)]="inputValue">
inputValue;
KeyUpCalled(value){
var dateCountTracker;
var currentDate = value;
var currentLength = currentDate.length;
var lastNumberEntered = currentDate[currentLength - 1];
if (currentLength > 10) {
var res = currentDate.substring(0, 10)
this.inputValue = res;
return this.inputValue
}
if (currentLength == 1 && currentDate > 1) {
var transformedDate = "0" + currentDate + '/';
dateCountTracker = 2;
currentLength = transformedDate.length;
this.inputValue = transformedDate;
return this.inputValue;
} else if (currentLength == 4 && currentDate[3] > 3) {
var transformedDate = currentDate.substring(0, 3) + "0" + currentDate[3] + '/';
dateCountTracker = 5;
currentLength = transformedDate.length;
this.inputValue = transformedDate;
return this.inputValue;
} else if (currentLength == 2 && (dateCountTracker != 2 && dateCountTracker != 3)) {
dateCountTracker = currentLength;
this.inputValue = currentDate + '/'
return this.inputValue;
} else if (currentLength == 5 && (dateCountTracker != 5 && dateCountTracker != 6)) {
dateCountTracker = currentLength;
// return currentDate + '/';
this.inputValue = currentDate + '/'
return this.inputValue;
}
dateCountTracker = currentLength;
this.inputValue = currentDate;
}
Using primeng :
in app module :
import {InputMaskModule} from 'primeng/inputmask';
#NgModule({
imports: [
...
InputMaskModule,
FormsModule
],
for the HTML :
<div class="p-col-12 p-md-6 p-lg-4">
<span>Date</span>
<p-inputMask mask="99/99/9999" [(ngModel)]="val3" placeholder="99/99/9999" slotChar="mm/dd/yyyy"></p-inputMask>
</div>
source : https://www.primefaces.org/primeng/inputmask
This might be very basic problem. I found the similar solution on Stack Overflow for formatting the phone number and I used it for asp:TextBox control, but I want this code to be work for multiple phone number textbox control rather than passing IDs directly. I have five different phone field and all those textbox are asp:TextBox. I want to call the same code from all those filed. (I am looking this solution in JavaScript only)
Here is my JS code:
/*Start of phone number formating */
var n;
var p;
var p1;
function format_phone() {
p = p1.value
if (p.length == 3) {
pp = p;
d4 = p.indexOf('(')
d5 = p.indexOf(')')
if (d4 == -1) {
pp = "(" + pp;
}
if (d5 == -1) {
pp = pp + ") ";
}
document.getElementById('<%=HomePhone.ClientID%>').value = "";
document.getElementById('<%=HomePhone.ClientID%>').value = pp;
}
if (p.length > 3) {
d1 = p.indexOf('(')
d2 = p.indexOf(')')
if (d2 == -1) {
l30 = p.length;
p30 = p.substring(0, 4);
p30 = p30 + ") "
p31 = p.substring(5, l30);
pp = p30 + p31;
document.getElementById('<%=HomePhone.ClientID%>').value = "";
document.getElementById('<%=HomePhone.ClientID%>').value = pp;
}
}
if (p.length > 7) {
p11 = p.substring(d1 + 1, d2);
if (p11.length > 4) {
p12 = p11;
l12 = p12.length;
l15 = p.length
p13 = p11.substring(0, 4);
p14 = p11.substring(4, l12);
p15 = p.substring(d2 + 1, l15);
document.getElementById('<%=HomePhone.ClientID%>').value = "";
pp = "(" + p13 + ") " + p14 + p15;
document.getElementById('<%=HomePhone.ClientID%>').value = pp;
}
l16 = p.length;
p16 = p.substring(d2 + 2, l16);
l17 = p16.length;
if (l17 > 3 && p16.indexOf('-') == -1) {
p17 = p.substring(d2 + 1, d2 + 5);
p18 = p.substring(d2 + 5, l16);
p19 = p.substring(0, d2 + 1);
pp = p19 + p17 + "-" + p18;
document.getElementById('<%=HomePhone.ClientID%>').value = "";
document.getElementById('<%=HomePhone.ClientID%>').value = pp;
}
}
setTimeout(format_phone, 100)
}
function getIt(m) {
n = m.name;
p1 = m;
format_phone()
}
/* End of phone number formating */
and asp:TextBox as
<asp:TextBox MaxLength="14"
runat="server" ID="HomePhone"
placeholder="(xxx) xxx-xxxx"
onFocus="if(this.value==this.defaultValue)this.value='';" onclick="javascript:getIt(this)"
onkeypress='return event.charCode >= 48 && event.charCode <= 57'/>
And I have other similar four textboxes for phone field and I want to use the same formatting logic for all those. What is the best way to use this or any alternative JavaScript code from multiple textbox. Any help would be highly appreciated.
I don't remember where I found this solution but, it might help you out to format the phone fields:
<script type="text/javascript">
//Phone validation
var zChar = new Array(' ', '(', ')', '-', '.');
var maxphonelength = 13;
var phonevalue1;
var phonevalue2;
var cursorposition;
function ParseForNumber1(object) {
phonevalue1 = ParseChar(object.value, zChar);
}
function ParseForNumber2(object) {
phonevalue2 = ParseChar(object.value, zChar);
}
function backspacerUP(object, e) {
if (e) {
e = e
} else {
e = window.event
}
if (e.which) {
var keycode = e.which
} else {
var keycode = e.keyCode
}
ParseForNumber1(object)
if (keycode >= 48) {
ValidatePhone(object)
}
}
function backspacerDOWN(object, e) {
if (e) {
e = e
} else {
e = window.event
}
if (e.which) {
var keycode = e.which
} else {
var keycode = e.keyCode
}
ParseForNumber2(object)
}
function GetCursorPosition() {
var t1 = phonevalue1;
var t2 = phonevalue2;
var bool = false
for (i = 0; i < t1.length; i++) {
if (t1.substring(i, 1) != t2.substring(i, 1)) {
if (!bool) {
cursorposition = i
bool = true
}
}
}
}
function ValidatePhone(object) {
var p = phonevalue1
p = p.replace(/[^\d]*/gi, "")
if (p.length < 3) {
object.value = p
} else if (p.length == 3) {
pp = p;
d4 = p.indexOf('(')
d5 = p.indexOf(')')
if (d4 == -1) {
pp = "(" + pp;
}
if (d5 == -1) {
pp = pp + ")";
}
object.value = pp;
} else if (p.length > 3 && p.length < 7) {
p = "(" + p;
l30 = p.length;
p30 = p.substring(0, 4);
p30 = p30 + ")"
p31 = p.substring(4, l30);
pp = p30 + p31;
object.value = pp;
} else if (p.length >= 7) {
p = "(" + p;
l30 = p.length;
p30 = p.substring(0, 4);
p30 = p30 + ")"
p31 = p.substring(4, l30);
pp = p30 + p31;
l40 = pp.length;
p40 = pp.substring(0, 8);
p40 = p40 + "-"
p41 = pp.substring(8, l40);
ppp = p40 + p41;
object.value = ppp.substring(0, maxphonelength);
}
GetCursorPosition()
if (cursorposition >= 0) {
if (cursorposition == 0) {
cursorposition = 2
} else if (cursorposition <= 2) {
cursorposition = cursorposition + 1
} else if (cursorposition <= 5) {
cursorposition = cursorposition + 2
} else if (cursorposition == 6) {
cursorposition = cursorposition + 2
} else if (cursorposition == 7) {
cursorposition = cursorposition + 4
e1 = object.value.indexOf(')')
e2 = object.value.indexOf('-')
if (e1 > -1 && e2 > -1) {
if (e2 - e1 == 4) {
cursorposition = cursorposition - 1
}
}
} else if (cursorposition < 11) {
cursorposition = cursorposition + 3
} else if (cursorposition == 11) {
cursorposition = cursorposition + 1
} else if (cursorposition >= 12) {
cursorposition = cursorposition
}
var txtRange = object.createTextRange();
txtRange.moveStart("character", cursorposition);
txtRange.moveEnd("character", cursorposition - object.value.length);
txtRange.select();
}
}
function ParseChar(sStr, sChar) {
if (sChar.length == null) {
zChar = new Array(sChar);
}
else zChar = sChar;
for (i = 0; i < zChar.length; i++) {
sNewStr = "";
var iStart = 0;
var iEnd = sStr.indexOf(sChar[i]);
while (iEnd != -1) {
sNewStr += sStr.substring(iStart, iEnd);
iStart = iEnd + 1;
iEnd = sStr.indexOf(sChar[i], iStart);
}
sNewStr += sStr.substring(sStr.lastIndexOf(sChar[i]) + 1, sStr.length);
sStr = sNewStr;
}
return sNewStr;
}
</script>
And call this on your asp:TextBox as
<asp:TextBox MaxLength="14"
runat="server" ID="HomePhone"
placeholder="(xxx) xxx-xxxx"
onkeydown="javascript:backspacerDOWN(this,event);"
onkeyup="javascript:backspacerUP(this,event);" />
And If you want to insert space after ')' you can use the following trick
function markSpace(field) {
if (field.value.includes(")")) {
field.value = field.value.split(')').join(') ');
}
if (field.value.includes(") ")) {
field.value = field.value.replace(/ +/g, ' ');
}
}
and call this as onblur="markSpace(this);" But I personally prefer using JQuery :)
The function I am using formats inserts '-' at certain points to auto format the date field. Right now it prints in the format 'mm-dd-yyyy', I am trying to get it to print 'yyy-mm-dd'.
<input type=textbox id=date onkeyup="dtval(this,event)" />
the js
function dtval(d,e) {
var pK = e ? e.which : window.event.keyCode;
if (pK == 8) {d.value = substr(0,d.value.length-1); return;}
var dt = d.value;
var da = dt.split('-');
for (var a = 0; a < da.length; a++) {if (da[a] != +da[a]) da[a] = da[a].substr(0,da[a].length-1);}
if (da[0] > 31) {da[1] = da[0].substr(da[0].length-1,1);da[0] = '0'+da[0].substr(0,da[0].length-1);}
if (da[1] > 12) {da[2] = da[1].substr(da[1].length-1,1);da[1] = '0'+da[1].substr(0,da[1].length-1);}
if (da[2] > 9999) da[1] = da[2].substr(0,da[2].length-1);
dt = da.join('-');
if (dt.length == 2 || dt.length == 5) dt += '-';
d.value = dt;
}
I think you'd benefit from the Date class.
var date1 = new Date('02-04-2016');
//Thu Feb 04 2016 00:00:00 GMT-0500 (Eastern Standard Time)
var date2 = date1.getFullYear() + '-'
+ ((date1.getMonth() + 1) > 9? date1.getMonth() + 1: '0' + (date1.getMonth() + 1)) + '-'
+ (date1.getDate() > 9? date1.getDate(): '0' + date1.getDate());
//"2016-02-04"
Here it is for YYYY-MM-DD:
function dtval(d,e) {
var pK = e ? e.which : window.event.keyCode;
if (pK == 8) {d.value = d.value.substr(0,d.value.length-1); return;}
var dt = d.value;
var da = dt.split('-');
for (var a = 0; a < da.length; a++) {
if (da[a] != +da[a]) da[a] = da[a].substr(0,da[a].length-1);
}
if (da[0] > 9999) {da[1] = da[0].substr(da[0].length-1,1);da[0] = '0'+da[0].substr(0,da[0].length-1);}
if (da[1] > 12) {da[2] = da[1].substr(da[1].length-1,1);da[1] = '0'+da[1].substr(0,da[1].length-1);}
if (da[2] > 31) da[1] = da[2].substr(0,da[2].length-1);
dt = da.join('-');
if (dt.length == 4 || dt.length == 7) dt += '-';
d.value = dt;
}
<input type=textbox id=date onkeyup="dtval(this,event)" />
BTW, you had this line:
if (pK == 8) {d.value = substr(0,d.value.length-1); return;}
There's no substr global function, it's a function of String:
if (pK == 8) {d.value = d.value.substr(0,d.value.length-1); return;}
I have a radtextbox with double type mode. And i have set round as 1. So it is giving the result as
20.6 => 20.5
20.7 => 20.5
20.9 => 21.0
But all i need is like this (few samples)
20.6 => 21.0
20.5 => 20.5
20.4 => 20.0
20.29 => 20.0
20.53 => 20.5
20.59 => 21
I tried to fix this by restricting the more than one decimal value. but the code is not working properly.
Code:
<script type="text/javascript">
function GetIndex(sender, args) {
var textbox = $find('<%= CPDPointsTextBox.ClientID %>');
var val = textbox.get_value();
var dsds = val.toString();
if (dsds.indexOf(".") > -1) {
if (dsds.length - (dsds.indexOf(".") + 1) > 1) {
alert(dsds.length - (dsds.indexOf(".") + 1));
args.set_cancel(true);
}
else
return true;
}
else {
if (parseInt(dsds) > 0) {
return true;
}
else
args.set_cancel(true);
}
}
<telerik:RadNumericTextBox ID="CPDPointsTextBox" Width="39px" runat="server" MaxLength="5"
MaxValue="999" MinValue="0">
<NumberFormat DecimalDigits="1" KeepNotRoundedValue="false" />
<ClientEvents OnValueChanged="CPDPointsTextBox_ValueChanged" OnKeyPress="GetIndex" />
</telerik:RadNumericTextBox>
You may use Jquery for this
var num = parseFloat(document.getElementById('textbox').value);
var new_num = Math.round(num).toFixed(2);
try here JsFiddle
I am using the following code. (May be bulkier).
function convertFloatDecimal (num, nDecimal)
{
var s;
if (nDecimal == null)
nDecimal = 1;
if (nDecimal == 1)
{
if (num < 0)
{
num= Math.abs (num);
s = ((Math.round (parseFloat (num) * 10)/10 ) * -1);
}
else
s = (Math.round (parseFloat (num) * 10)/10);
s = s.toString();
if (s.indexOf (".") == -1)
s+= ".0";
}
else if (nDecimal == 2)
{
if (num < 0)
{
num= Math.abs (num);
s = ((Math.round (parseFloat (num) * 100) / 100) * -1);
}
else
s = (Math.round (parseFloat (num) * 100) / 100);
s = s.toString();
if (s.indexOf (".") == -1)
s+= ".00";
if (s.indexOf (".") == s.length-2) // Add leading 0
s+= "0";
}
else if (nDecimal == 0)
{
if (num < 0)
{
num= Math.abs (num);
s = (Math.round (num) * -1);
}
else
s = (Math.round (num));
}
else
{
s = parseInt (s);
}
return s;
}
The following code meets the expectation. Thank you for all your replies.
Code:
function change(lnk, evt) {
var textbox = $find('<%= CPDTextBox.ClientID %>');
var val = lnk.value.toString();
if (val != '') {
if (val.indexOf(".") > -1) {
var values = new Array();
values = val.split(".");
var rouDec = round(parseFloat('.' + values[1]), 1);
if (rouDec > .5) {
val = parseInt(values[0]) + 1;
}
else if (rouDec == .5) {
val = parseInt(values[0]) + .5;
}
}
}
textbox.set_value(val);
}
function round(n, dec) {
n = parseFloat(n);
if (!isNaN(n)) {
if (!dec) var dec = 0;
var factor = Math.pow(10, dec);
return Math.floor(n * factor + ((n * factor * 10) % 10 >= 5 ? 1 : 0)) / factor;
} else {
return n;
}
}