Text replace
-
I have a text box andit will accept only time in the format 00:00(maximum value can be 12:59) . If user enter any of the value greater than this that part only needs to become 00. i have the code like this,but it will change the whole value to 00:00 . In the greater than conditions, what i have to write .
function ValidateTimeControl(s, e) {
var isValid = true;
if (e.value != null) {
if (e.value.substring(0, 1).trim() == "" || e.value.substring(1, 2).trim() == "" || e.value.substring(3, 4).trim() == "" || e.value.substring(3, 4).trim() == "") {
isValid = false;
}
else {
var hours = parseInt(e.value.substring(0, 2), 10);
var minutes = parseInt(e.value.substring(3, 5), 10);if (hours > 12) {alert(hours) isValid = false; } if (minutes > 59) { isValid = false; } } } else { isValid = false; } if (!isValid) { e.value = '00:00'; }
}
Thanks Rks
-
I have a text box andit will accept only time in the format 00:00(maximum value can be 12:59) . If user enter any of the value greater than this that part only needs to become 00. i have the code like this,but it will change the whole value to 00:00 . In the greater than conditions, what i have to write .
function ValidateTimeControl(s, e) {
var isValid = true;
if (e.value != null) {
if (e.value.substring(0, 1).trim() == "" || e.value.substring(1, 2).trim() == "" || e.value.substring(3, 4).trim() == "" || e.value.substring(3, 4).trim() == "") {
isValid = false;
}
else {
var hours = parseInt(e.value.substring(0, 2), 10);
var minutes = parseInt(e.value.substring(3, 5), 10);if (hours > 12) {alert(hours) isValid = false; } if (minutes > 59) { isValid = false; } } } else { isValid = false; } if (!isValid) { e.value = '00:00'; }
}
Thanks Rks
You need to rewrite the text value with the hours or minutes set to zero. Something like:
else {
var hours = parseInt(e.value.substring(0, 2), 10);
var minutes = parseInt(e.value.substring(3, 5), 10);if (hours > 12) { alert(hours) hours = 0; } if (minutes > 59) { minutes = 0; } // I'm not sure about this but you can probably figure out the exact statement e.value = hours.toString() + ":" + minutes.toString();
}
One of these days I'm going to think of a really clever signature.
-
You need to rewrite the text value with the hours or minutes set to zero. Something like:
else {
var hours = parseInt(e.value.substring(0, 2), 10);
var minutes = parseInt(e.value.substring(3, 5), 10);if (hours > 12) { alert(hours) hours = 0; } if (minutes > 59) { minutes = 0; } // I'm not sure about this but you can probably figure out the exact statement e.value = hours.toString() + ":" + minutes.toString();
}
One of these days I'm going to think of a really clever signature.
Thanks for the reply . i Found a solution like this
function ValidateTimeControl(s, e) {
var isValidHour = true;
var isValidMin = true;
var hours, minutes;
if (e.value != null) {
if (e.value.substring(0, 1).trim() == "" || e.value.substring(1, 2).trim() == "" ) {
isValidHour = false;
}
if (e.value.substring(3, 4).trim() == "" || e.value.substring(3, 4).trim() == "") {
isValidMin = false;
}
else {
hours = parseInt(e.value.substring(0, 2), 10);
minutes = parseInt(e.value.substring(3, 5), 10);if (hours > 12) {alert(hours) isValidHour = false; } if (minutes > 59) { isValidMin = false; } } } else { isValidHour = false; isValidMin = false; } if (isValidHour == false && isValidMin == false) { e.value = '00:00'; } else if (isValidHour == false) { e.value = e.value.replace(hours, '00'); } else if (isValidMin == false) { e.value = e.value.replace(minutes, '00'); }
}