正则表达式验证和跨域postmessage

1.用正则表达式验证用户名

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form>
        <input type="text" id="username" name="username" pattern="[A-Za-z0-9_-]{6,16}" required>
        <input type="submit" value="提交">
        <div class="error-message">用户名必须由6到16个字符的字母、数字组成。</div>
      </form>
</body>
</html>

解释该正则:

  • [a-zA-Z0-9_-]:匹配任意字母、数字、下划线或破折号。
  • {3,16}:限制用户名的长度在6到16个字符之间。

2.跨域postmessage

需要用到两个虚拟主机

子页面:

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>2023</title>
</head>
<body>
    <div>
        <h1>2024.security.pw</h1>
    </div>
</body>
<script>
    window.addEventListener('message', (event) => {
        if (event.origin === 'http://2024.oupeng.pw') {
            const cookieData = event.data;
            //处理cookieData
            console.log('Receive message from parent:', cookieData);
            window.parent.postMessage('child message', '*');
        }
    })
</script>

</html>

父页面:

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>iframe</title>
</head>
<body>
    <iframe id="myIframe" src="http://2024.security.pw/" frameborder="0"></iframe>
</body>
<script>
    window.onload = function() {
        document.cookie = 'sessionid=oupeng'
        const cookieData = document.cookie
        window.frames[0].postMessage(cookieData, 'http://2024.security.pw/');
    }
    //添加一个监听事件处理子页面的返回消息
    window.addEventListener('message', (event) => {
        if(event.origin === 'http://2024.security.pw')
        console.log('Received message from child:', event.data);
    })
</script>
</html>

验证:在子页面里接受到返回值和发送消息到子页面,实现跨域

相关推荐
吴永琦(桂林电子科技大学)1 小时前
HTML5
前端·html·html5
爱因斯坦乐1 小时前
【HTML】纯前端网页小游戏-戳破彩泡
前端·javascript·html
与妖为邻9 小时前
自动获取屏幕尺寸信息的html文件
前端·javascript·html
try again!11 小时前
HTML快速上手
前端·css·html
ElasticPDF-新国产PDF编辑器13 小时前
HTML && jQuery 项目 PDF 批注插件库在线版 API 示例教程
pdf·html·jquery
我自纵横202316 小时前
使用 JavaScript 动态设置 CSS 样式
开发语言·前端·javascript·css·html·json·html5
小破孩呦17 小时前
动态循环表单+动态判断表单类型+动态判断表单是否必填方法
前端·javascript·html
慕斯策划一场流浪1 天前
fastGPT—前端开发获取api密钥调用机器人对话接口(HTML实现)
前端·html·fastgpt·fastgpt创建工作流·fastgpt创建密钥调用接口
碧海饮冰1 天前
前端:v-html和v-text在使用上的区别
前端·javascript·html
CD-i1 天前
用HTML.CSS.JavaScript实现一个贪吃蛇小游戏
javascript·css·html