我需要使用npm test
输入(type =" email"等),但我还需要显示自己的错误消息,我可以在CSS中设置样式。
HTML5

document.getElementById('myForm').onsubmit = function() {
document.getElementById('message').innerHTML = '<div>Email must be valid.</div>';
return false;
}
&#13;
#message > div {
font-family: Open Sans, Arial;
font-size: 200%;
position: absolute;
left: 50%;
letter-spacing: 0.05em;
top: 50%;
transform: translate(-50%, -50%);
}
&#13;
有没有人知道如何在HTML5验证之前运行我自己的脚本?我只能想到onclick按钮事件,然后点击进入,但它并不理想。
答案 0 :(得分:2)
如果您想进行自己的验证,请在表单中添加import re
import cgi
import webapp2
# html boilerplate for the top of every page
page_header = """
<!DOCTYPE html>
<html>
<head>
<title>User-Signup</title>
</head>
<body>
<h1>Signup</h1>
"""
# html boilerplate for the bottom of every page
page_footer = """
</body>
</html>
"""
USER_RE = re.compile(r"^[a-zA-Z0-9_-]{3,20}$")
def valid_username(username):
return username and USER_RE.match(username)
PASS_RE = re.compile(r'^[\S]+@[\S]+\.[\S]+$')
def valid_password(password):
return password and PASS_RE.match(password)
EMAIL_RE = re.compile(r'^[\S]+@[\S]+\.[\S]+$')
def valid_email(email):
return not email or EMAIL_RE.match(email)
class MainHandler(webapp2.RequestHandler):
def get(self):
add_username = """
<form action="/" method="post">
<label>
Username <input type="text" name="user_name" value = ""/>
</label>
</form>
"""
add_password = """
<form action="/" method="post">
<label>
Password <input type="password" name ="password" value = ""/>
</label>
</form>
"""
validate_password = """
<form action="/" method="post">
<label>
Password <input type="password" name ="password" value = ""/>
</label>
</form>
"""
# error = self.request.get("error")
# if error:
# error_esc = cgi.escape(error, quote=True)
# error_element = '<p class="error">' + error_esc + '</p>'
# else:
error_element = ''
password_error = ''
content = page_header + add_username + error_element + add_password + password_error + page_footer
self.response.write(content)
def post(self):
error_element = ''
have_error = False
user_name = self.request.get("user_name")
password = self.request.get("password")
params = dict("")
if not valid_username(user_name):
error_element = "thats not a valid username"
have_error = True
if not valid_password(password):
password_error = "thats not a valid password"
have_error = True
add_username = """
<form method="post">
<label>
Username <input type="text" name="user_name" value = "{0}"/>
</label>
</form>
""".format(user_name)
add_password = """
<form method="post">
<label>
Password <input type="password" name ="password" value = "{0}"/>
</label>
</form>
""".format(password)
validate_password = """
<form action="/" method="post">
<label>
Password <input type="password" name ="password" value = "{0}"/>
</label>
</form>
""".format(password_error)
content = page_header + add_username + error_element + add_password + password_error + page_footer
self.response.write(content)
属性,以防止浏览器执行html5内部验证
novalidate
答案 1 :(得分:0)