我验证了输入字段以检查空字段。后来我注意到有人可以输入空格并提交表格。有人可以帮我删除文本前的空白区域,但接受输入文本中间的空格。我尝试使用它但不允许输入中间的空格
$('input').keyup(function() {
$(this).val($(this).val().replace(/\s+/g, ''));
});
我该如何解决这个问题?
答案 0 :(得分:1)
首先,与您的问题无关,但您应该仅在用户尝试提交表单时验证该字段,因此,您应该使用HTMLFormElement' s {{ 1}}事件。
其次,你应该在你的后端重新验证你的表单,因为人们可以通过控制台/调试等改变客户端的东西,这确实是一个安全问题。
第三,关于你的问题本身:你可以使用String.prototype.trim
方法,如下所示:
submit
答案 1 :(得分:0)
.trim()删除前导和尾随空格,使中间的空格不受干扰。
答案 2 :(得分:0)
你需要修剪方法。简短的例子:
#qpy:kivy
'''
Created on 20.06.2015
@author: jan
'''
import kivy
kivy.require('1.0.7') # replace with your current kivy version !
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
#from kivy.core.audio import SoundLoader
from jnius import autoclass
import os
import time
if not os.path.isdir("/sdcard/kivyrecords/"):
os.mkdir("/sdcard/kivyrecords/")
PATH = "/sdcard/kivyrecords/rec{0}.mp4"
class Player(object):
def __init__(self):
# get the MediaPlayer java class
self.MediaPlayer = autoclass('android.media.MediaPlayer')
def play(self, button):
# create our player
mPlayer = self.MediaPlayer()
mPlayer.setDataSource(PATH.format(button))
mPlayer.prepare()
mPlayer.start()
if not mPlayer.isPlaying():
mPlayer.release()
class Recorder(object):
def __init__(self):
# get the needed Java classes
self.MediaRecorder = autoclass('android.media.MediaRecorder')
self.AudioSource = autoclass('android.media.MediaRecorder$AudioSource')
self.OutputFormat = autoclass('android.media.MediaRecorder$OutputFormat')
self.AudioEncoder = autoclass('android.media.MediaRecorder$AudioEncoder')
self.mRecorder = self.MediaRecorder()
def start(self, button):
self.mRecorder.setAudioSource(self.AudioSource.MIC)
self.mRecorder.setOutputFormat(self.OutputFormat.MPEG_4)
self.mRecorder.setOutputFile(PATH.format(button))
self.mRecorder.setAudioEncoder(self.AudioEncoder.HE_AAC)
self.mRecorder.prepare()
self.mRecorder.start()
def stop(self, button):
time.sleep(0.5)
self.mRecorder.stop()
self.mRecorder.release()
self.mRecorder = self.MediaRecorder()
PLAY = Player()
REC = Recorder()
RECORDED = {1 : False, 2 : False, 3 : False,
4 : False, 5 : False, 6 : False,
7 : False, 8 : False, 9 : False}
class MyGrid(GridLayout):
def __init__(self):
super(MyGrid, self).__init__(cols=3, rows=3)
global REC
global RECORDED
global PLAY
for i in range(1, 10):
b = Button(text=str(i))
b.bind(on_press=self.recorplay)
b.bind(on_release=self.stop)
self.add_widget(b, i)
def recorplay(grid, button):
if not RECORDED[int(button.text)]:
REC.start(button.text)
else:
PLAY.play(button.text)
def stop(grid, button):
if not RECORDED[int(button.text)]:
REC.stop(button.text)
RECORDED[int(button.text)] = True
class MyApp(App):
def build(self):
return MyGrid()
if __name__ == '__main__':
MyApp().run()
警报将显示没有空格的Hello World。
答案 3 :(得分:0)
你可以使用String trim()方法 Reference is here