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

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

相关推荐
数字芯片实验室5 小时前
分享一个可以学习正则表达式的网址:Pythex.org
学习·正则表达式
兴趣使然_9 小时前
【笔记】使用 html 创建网址快捷方式
笔记·html·js
Zachery Pole14 小时前
BootStrap
前端·bootstrap·html
淮北49415 小时前
最简单的实验室资产管理系统,使用Flask,mysql,html(四、知识补充)
mysql·flask·html
多啦C梦a16 小时前
【前端必修】闭包、`this`、`箭头函数`、`bind`、节流,一篇文章全懂!
前端·javascript·html
Canaan28516 小时前
前端开发-标签
html
爱编程的喵18 小时前
深入理解JavaScript节流函数:从原理到实战应用
前端·javascript·html
web守墓人1 天前
【前端】ikun-markdown: 纯js实现markdown到富文本html的转换库
前端·javascript·html
阿蒙Amon1 天前
C#正则表达式全面详解:从基础到高级应用
开发语言·正则表达式·c#
国家不保护废物2 天前
多模态模型数据传输的秘密武器:html5对象Blob深度解析
前端·面试·html