使用HTML限制为适当的十进制数字,但不包含字母

问题描述

我尝试使用此代码

#%pip install keras-metrics

# Importing required packages
import keras_metrics as km

# LSTM Workings_Autoencoder Model

ac_model_1b = Sequential()
ac_model_1b.add(Bidirectional(LSTM(units=200,return_sequences = True,input_shape = (n_timesteps,n_features),kernel_initializer='glorot_normal')))
ac_model_1b.add(LSTM(100))
ac_model_1b.add(Dropout(0.2))

ac_model_1b.add(RepeatVector(n_timesteps))

ac_model_1b.add(LSTM(100,return_sequences = True))
ac_model_1b.add(Dropout(0.2))
ac_model_1b.add(LSTM(200,return_sequences = True))
ac_model_1b.add(Timedistributed(Dense(1,activation='sigmoid')))

ac_model_1b.compile(loss='binary_crossentropy',optimizer='Adamax',metrics=['accuracy',km.binary_precision(),km.binary_recall()])

results_ac_model_1b = ac_model_1b.fit(x_train,y_train,epochs=100,batch_size=32,shuffle=True,validation_data=(x_valid,y_valid))

print(ac_model_1b.summary())

ac_model_1b.save('lstm_model_adamax.h5')

但是可悲的是,这仍然允许1567.5432.5678.4567(多个小数点)。

是否可以将其限制为仅小数点后1位,但无限制的小数位或有效数字?

解决方法

使用正则表达式仅使用数字值和一个小数。

^(\d+)?([.]?\d{0,2})?$

如果您想使用较大的数字(即数字中小数点右边的位数),请在此处更改数字\ d {0,2}

上面的表达式允许十进制数字介于零到2之间。

,

使用正则表达式限制小数点。

/\d+\.?\d*/

例如

'123.675.009'.match(/\d+\.?\d*/)

返回123.675

,

我最终使用了这种方法:

function isNumberKey(evt){
    var charCode = (evt.which) ? evt.which : evt.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46)
        return false;
    return true;
    if (charCode === 46) {

    // Allow only 1 decimal point
    if ((element.value) && (element.value.indexOf('.') >= 0))
      return false;
    else
      return true;
  }
}

这可以解决字母和多于一个小数位的输入。