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

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

相关推荐
wsad05321 小时前
Vue.js 整合传统 HTML 项目:注册页面实战教程
前端·vue.js·html
老师好,我是刘同学1 小时前
Python正则表达式re模块全解析
python·正则表达式
我是初九2 小时前
【遇见狂神说|前端】HTML5
前端·html
a1117762 小时前
堆叠式流程图编辑器(html 开源)
开发语言·前端·javascript·开源·编辑器·html·流程图
kyriewen3 小时前
我敢打赌,你还不知道 display 还有这些骚操作!
前端·css·html
我命由我123454 小时前
React - ref、回调 ref 回调执行次数的问题、createRef 函数、事件处理
前端·javascript·react.js·前端框架·html·html5·js
我命由我123454 小时前
React - 收集表单元素、收集表单元素优化、生命周期(旧)、生命周期(新)
前端·javascript·react.js·前端框架·html·html5·js
豆豆4 小时前
PageAdmin CMS模板开发详解:HTML转CMS系统的10个核心步骤
前端·html·cms·网站建设·网站制作·自助建站·网站管理系统
Never_Satisfied4 小时前
在JavaScript / HTML中,判断指定的元素是否含有某个类
开发语言·javascript·html
下北沢美食家4 小时前
HTML面试题
前端·html