我有一个表单,允许用户提供名字和姓氏、电子邮件和部门。该表单是用jsp编写的,我想修改电子邮件输入字段,使其不允许用户在输入电子邮件时添加任何空格(即,如果他们尝试添加空格,类(class)器将保留在最开始,也不允许输入电子邮件后的空格)。
我在 Google 上浏览寻找解决方案,发现使用 javaScript 和 jQuery 非常简单,但不幸的是我对此了解不多,因为大多数时候我都是用 java 编写代码。
这是我的 jsp 表单中的代码片段
<div class="formFields">
<div class="formFieldsTop">
<input type="hidden" name="CSRF_TOKEN" value="<%= pageBean.getCsrfToken()%>">
<label id="firstNameTxt" class="boldText">First Name</label>
<input class="requiredInput subUserFirstNameInput" name="firstName" type="textfield" size="30" maxlength="15" autocomplete="off" value="<%= NullSafe.subst(EcommerceFormat.escapeHtmlSafeString(delegateInfo.getFirstName()))%>">
<label id="lastNameTxt" class="boldText">Last Name</label>
<input class="requiredInput" name="lastName" id="subUserLastNameInput" autocomplete="off" type="textfield" size="30" maxlength ="20" autocomplete="off" value="<%= NullSafe.subst(EcommerceFormat.escapeHtmlSafeString(delegateInfo.getLastName()))%>">
<label id="emailTxt"><span class="boldText">Email Address*</span> (ex: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="22474f434b4e625b4d5750514b56470c414d4f" rel="noreferrer noopener nofollow">[email protected]</a>)</label>
<input class="requiredInput subUserEmailInput" name="email" id="subUserEmailInput" type="textfield" size="30" autocomplete="off" maxlength="50" value="<%= NullSafe.subst(EcommerceFormat.escapeHtmlSafeString(delegateInfo.getEmailAddress()))%>">
<label id="departmentTxt"><span class="bold">Department/Relationship</span> (optional)</label>
<input name="department" type="textfield" size="30" autocomplete="off" maxlength="100" value="<%= NullSafe.subst(EcommerceFormat.escapeHtmlSafeString(delegateInfo.getDepartmentCode()))%>">
</div>
</div>
我想知道是否有办法仅使用jsp或结合jsp、js和jQuery来阻止用户在电子邮件字段中添加空格?
最佳答案
您可以尝试将类型属性添加到电子邮件输入和必填
<input type="email" required>
或将模式属性添加到电子邮件输入
<input pattern="[a-z\._\d]{1,}@[a-z0-9]{2,}\.[a-z0-9]{2,}" required>
关于javascript - 如何防止用户在电子邮件输入字段中添加空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57422595/