How to validate with Javascript an Input text with Hours and Minutes

后端 未结 8 1034
灰色年华
灰色年华 2020-12-28 13:49

I have to build an HTML Form with an Input text field for Hours and Minutes.
Something like:

Name : [Foo]
Surname

相关标签:
8条回答
  • 2020-12-28 14:18

    You can use something like jQuery.maskedit

    0 讨论(0)
  • 2020-12-28 14:20

    You can use this function to check for time format:

    function validateTime (time) {
      const timeReg = /^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/
      return time.match(timeReg)
    }
    

    If you want to have just hour and minute please replace timeReg with this:

    const timeReg = /^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/
    

    And this one for the date format:

    function validateDate (date) {
      try {
        new Date(date).toISOString()
        return true
      } catch (e) {
        return false
      }
    }
    
    0 讨论(0)
提交回复
热议问题