Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

text item SUBSTRING

Status
Not open for further replies.

Scunningham99

Programmer
Sep 20, 2001
815
0
0
GB
HI all

I have a text itemwith a size of 4. the user enters a time in here for example 1055. I wish to substring this out for example the first to characters to be the hours and the second two the minutes. Is this possible.

Thanks in advance
 
var hh = formVar.substr(0,2);
var mm = formVar.substr(2,2);

Of course the above assumes that user entered 4 digits. You may want to check the length or use the length of the string as part of the substr call.

Go to and click on "Learn JavaScript" to learn about javascript. You can also try out your coding at that site.

Dan
 
Here is a time checker example:

<script id=&quot;time checking function&quot;>
function isTime(objTbox,blnCanBeNull,strControl,strErrLabel,blnRange,strMin,strMax,strTimeFormat) {
// objTbox: is the textbox that contains the value that needs to be checked you
// can pass this using the following command: document.getElementById('textboxid')
// bnlCanBeNull: if this value is true than the textbox can be empty if it is false the textbox cannot be empty
// strControl: This is the name of how the user preceives the textbox for example:
// starttime: **some textbox*** The user will see the textbox as starttime
// because the text starttime is before the textbox
// strErrLabel: This is the label where the errormessage wil be displayed
// blnRange: This is true if the time needs to be in a certain range
// for instance between 8:00 and 22:00
// strMin If the time needs to be in a certain range than strMin is the
// minimum vale for example a time between 8:00 and 22:00 the 8:00
// wil be the minimum value !!!IN STRMIN THE : IS LEFT OUT AND IT NEEDS TO BE 4 CHARACTERS LONG!!!
// so in this example you have to specify strMin as 0800
// strMax If the time needs to be in a certain range than strMax is the
// maximum time, given the example used in strMin strMax wil be 2200
// strTimeFormat: This is the format of the time, if you need a pm/am time format you
// have to provide a string &quot;pm/am&quot;. If it is empty a 24 hour timeformat
// is used
var str24H = &quot;&quot;;
var strTimeSuffix = &quot;&quot;;
var strMaxHourSuffix = &quot;&quot;
var strMinHourSuffix = &quot;&quot;
var blnChecking = true;
var strError = &quot;&quot;;
var i = 0;
var strHour = &quot;&quot;;
var strMinute = &quot;&quot;
var strMinMinute = &quot;&quot;;
var strMinHour = &quot;&quot;;
var strMaxMinute = &quot;&quot;;
var strMaxHour = &quot;&quot;;
var strCurrentValue = objTbox.value;
if((blnCanBeNull)&&objTbox.value==&quot;&quot;){
return true;
}
if((!blnCanBeNull)&&objTbox.value==&quot;&quot;){
strError = 'Please fill out a correct time in the &quot;' + strControl + '&quot; box.'
blnChecking = false;
}
for(i=0;i<objTbox.value.length;i++){
if((!isNaN(objTbox.value.charAt(i))&&objTbox.value.charAt(i)!=&quot; &quot;)){
strHour = strHour + objTbox.value.charAt(i).toString();
}
if(strHour.length==2){
i++;
break;
}
if(strHour.length>0&&(isNaN(objTbox.value.charAt(i))||objTbox.value.charAt(i)==&quot; &quot;)){
i++;
break;
}
}
while(i<objTbox.value.length){
if((!isNaN(objTbox.value.charAt(i))&&objTbox.value.charAt(i)!=&quot; &quot;)){
strMinute = strMinute + objTbox.value.charAt(i).toString();
}
if(strMinute.length==2){
break;
}
if(strMinute.length>0&&(isNaN(objTbox.value.charAt(i))||objTbox.value.charAt(i)==&quot; &quot;)){
break;
}
i++;
}
if(strHour.length==0||strMinute.length==0){
strError = 'Please fill out a correct time in the &quot;' + strControl + '&quot; box.'
blnChecking = false;
}
if(strHour.length==1&&blnChecking){
strHour = &quot;0&quot; + strHour;
}
if(strMinute.length==1&&blnChecking){
strMinute = &quot;0&quot; + strMinute;
}
if(strTimeFormat.toLowerCase()!=&quot;pm/am&quot;&&blnChecking){
if(parseInt(strHour,10)>23||parseInt(strHour,10)<0){
strError = 'Please fill out a correct time in the &quot;' + strControl + '&quot; box.'
blnChecking = false;
}
}else if(blnChecking){
if(parseInt(strHour,10)>12||parseInt(strHour,10)<1){
strError = 'Please fill out a correct time in the &quot;' + strControl + '&quot; box.\nTime format is PM/AM for example: 12:00 PM'
blnChecking = false;
}
}
if((parseInt(strMinute,10)>59||parseInt(strMinute,10)<0)&&blnChecking){
strError = 'Please fill out a correct time in the &quot;' + strControl + '&quot; box.'
blnChecking = false;
}
if(blnRange&&blnChecking){
if(strTimeFormat.toLowerCase()!=&quot;pm/am&quot;){
str24H = parseInt((strHour.toString() + strMinute.toString()),10);
strMinMinute = strMin.substring(2,4);
strMinHour = strMin.substring(0,2);
strMaxMinute = strMax.substring(2,4);
strMaxHour = strMax.substring(0,2);
}else{
var intTmp = 0;
if(objTbox.value.substring(objTbox.value.length-2,objTbox.value.length).toLowerCase()!=&quot;am&quot;){
intTmp = 12;
}
str24H = parseInt((parseInt(strHour,10)+intTmp).toString() + strMinute.toString(),10);
strMinMinute = strMin.substring(2,4);
if(parseInt(strMin.substring(0,2),10)>12){
strMinHour = parseInt(strMin.substring(0,2),10)-12;
strMinHourSuffix = &quot; PM&quot;
}else{
strMinHour = parseInt(strMin.substring(0,2),10);
strMinHourSuffix = &quot; AM&quot;
}
strMaxMinute = strMax.substring(2,4);
if(parseInt(strMax.substring(0,2),10)>12){
strMaxHour = parseInt(strMax.substring(0,2),10)-12;
strMaxHourSuffix = &quot; PM&quot;
}else{
strMaxHour = parseInt(strMax.substring(0,2),10);
strMaxHourSuffix = &quot; AM&quot;
}
}
if(str24H<strMin||str24H>strMax){
strError = &quot;Please use a time between &quot; + strMinHour + &quot;:&quot; + strMinMinute + &quot; &quot; + strMinHourSuffix + &quot; and &quot; + strMaxHour + &quot;:&quot; + strMaxMinute + strMaxHourSuffix
blnChecking=false
}
}
if(blnChecking){
objTbox.style.color = &quot;green&quot;;
document.getElementById(strErrLabel).innerHTML = &quot;&quot;;
if(strTimeFormat.toLowerCase()==&quot;pm/am&quot;){
var intTmp = 0;
if(objTbox.value.substring(objTbox.value.length-2,objTbox.value.length).toLowerCase()!=&quot;am&quot;){
strTimeSuffix=&quot; PM&quot;;
}else{
strTimeSuffix=&quot; AM&quot;;
}
objTbox.value= strHour + &quot;:&quot; + strMinute + strTimeSuffix;
}else{
objTbox.value= strHour + &quot;:&quot; + strMinute
}
}else{
objTbox.style.color = &quot;red&quot;;
document.getElementById(strErrLabel).innerHTML = &quot;<font color=red>&quot; + strError + &quot;</font>&quot;;
}
return blnChecking;
}
</script>
test text box: 24h<input type=text maxlength=5 id=txttext name=txttext><br>
<input type=button value='test script' onclick=&quot;isTime(eval('txttext'),true,'test text box: 24h','lblValidate',true,'0700','2200','24h')&quot; id=button1 name=button1><br>


test text box: PM/AM<input type=text maxlength=8 id=txtpmtext name=txtpmtext><br>
<input type=button value='test script' onclick=&quot;isTime(eval('txtpmtext'),false,'test text box: PM/AM','lblValidate',true,'0700','2200','pm/am')&quot; id=button1 name=button1><br>
<br>
<LABEL id=lblValidate></LABEL>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top