0

I want take time value as input and i want to make : fixed while entering input.

For example: 12:23:24 while entering this value as input i want to make the colon fixed and not editable. I am working on jsp and jquery and my backend is in java. Is there a way possible?

<input type="text" name="test_tat1" onclick="changecolor6665()" id="tat"style="border-color: darkgray;" class="form-control p-input test_tat "aria-describedby="labtest_name" autocomplete="off"   placeholder="Test Tat" value="${tatV}" >

This is my input field.

4
  • 1
    There is no support of JSP for that. But you can achieve that functionality by jQuery or JS. Commented Nov 25, 2019 at 4:43
  • can you give me a code for that with jqery or js? Commented Nov 25, 2019 at 5:04
  • But I am curious to know as the whole point is to take the time value from the input, why aren't you using any time picker plugin? Commented Nov 25, 2019 at 5:12
  • when i use time picker i am getting it as HH:MM am/pm format and what i want is a time value in hh:mm:ss format which a user can enter as he/she wishes Commented Nov 25, 2019 at 9:51

1 Answer 1

0

As said, there is no support for this with JSP.

I guess you want something like this as your jQuery/javascript:

$("input[name='masknumber']").on("keyup", function(){
    $("input[name='number']").val(destroyMask(this.value));
this.value = createMask($("input[name='number']").val());
})

function createMask(string){
    console.log(string)
return string.replace(/(\d{2})(\d{2})(\d{2})/,"$1:$2:$3");
}

function destroyMask(string){
    console.log(string)
return string.replace(/\D/g,'').substring(0, 6);
}

With this as your HTML:

<input type="text" name="masknumber">
<input type="text" name="number" style="display:none;">

Found similar here

EDIT

Did you insert the jQuery, right?

This one is working for me:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>

<input type="text" name="masknumber">
<input type="text" name="number" style="display:none;">

<script>    
$("input[name='masknumber']").on("keyup", function(){
    $("input[name='number']").val(destroyMask(this.value));
this.value = createMask($("input[name='number']").val());
})

function createMask(string){
    console.log(string)
return string.replace(/(\d{2})(\d{2})(\d{2})/,"$1:$2:$3");
}

function destroyMask(string){
    console.log(string)
return string.replace(/\D/g,'').substring(0, 8);
}   
</script>
</body>
</html>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help. Its working. Not in a way i thought but still it will help me complete my work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.