正则表达式验证和跨域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>

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

相关推荐
AnalogElectronic1 小时前
html+js+css实现七龙珠神龙召唤特效
javascript·css·html
LXXgalaxy2 小时前
HTML头部元信息避坑指南
前端·html
不恋水的雨2 小时前
html中补齐table表格合并导致每行td数量不一致的情况
前端·html
iReachers2 小时前
HTML打包EXE工具四种弹窗方式图文详解 - 单窗口/新窗口/标签页/浏览器打开
前端·javascript·html·弹窗·html打包exe·html转程序
ZC跨境爬虫21 小时前
海南大学交友平台登录页开发实战day6(覆写接口+Flask 本地链接正常访问)
前端·后端·python·flask·html
iReachers1 天前
HTML打包EXE三种加密方式对比:静态密码、离线一机一码、网络验证
html·html转exe·html打包exe·html一键打包exe工具
05Nuyoah1 天前
第一阶段:HTML的笔记
前端·笔记·html
华科大胡子2 天前
HTML头部元信息避坑
html
2401_827499992 天前
python项目实战11-正则表达式基础
python·mysql·正则表达式
互联网散修2 天前
零基础鸿蒙应用开发第三十三节:正则表达式基础与应用
华为·正则表达式·harmonyos