Validating URLs(验证url)

本文探讨了URL的有效性验证标准及其在网页表单中的应用。文章指出不同浏览器对URL前缀的要求有所不同,并提供了使用HTML表单及正则表达式增强URL验证的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如果你问什么是一个有效的URL你也肯定会得到不同的答案取决于你问谁。这影响到url输入类型和手段,尽管大多数浏览器url前缀的需求你可以做一张自己和两个http://和book:/ /将被接受为有效的url前缀。(其实,你唯一需要的是一封信,试一试自己的即时预览验证器下面)。另一方面,没有空间是允许的,但你的url不需要包括一个时期。我希望明确表示,不能完全依赖于url输入验证然而,但它仍然是一个好主意,把它作为它的未来证明你的网站。

<form>
        <input type="url" required /> 
        <input type="submit" value="Submit Now!">
</form>

幸运的是,我们可以改善url验证inout类型通过添加模式属性来我们的输入字段。pattern属性接受JavaScript正则表达式。如果您不熟悉正则表达式,所有你需要知道的现在,就是你可以添加以下属性:模式=“https ?:/ /。+”到你的输入字段,然后你提高了你的url验证,是要求所有的url的开始或者http://或https://

<form>
        <input type="url" pattern="https?://.+" required /> 
        <input type="submit" value="Submit Now!">
</form>


[File Watcher (node.js)] Request to start watching: /home/SENSETIME/leisiquan.vendor/.config/Code/User (excludes: <none>, includes: <all>, filter: <none>, correlationId: <none>),/home/SENSETIME/leisiquan.vendor/.config/Code/User/settings.json (excludes: <none>, includes: <all>, filter: <none>, correlationId: <none>) [main 2025-08-01T12:47:02.252Z] [File Watcher (node.js)] ignoring a path for watching who's stat info failed to resolve: /home/SENSETIME/leisiquan.vendor/.config/Code/User/settings.json (error: Error: ENOENT: no such file or directory, stat '/home/SENSETIME/leisiquan.vendor/.config/Code/User/settings.json') [main 2025-08-01T12:47:02.253Z] [File Watcher (node.js)] starting fs.watchFile() on /home/SENSETIME/leisiquan.vendor/.config/Code/User/settings.json (correlationId: undefined) [main 2025-08-01T12:47:02.256Z] Starting VS Code [main 2025-08-01T12:47:02.256Z] from: /usr/share/code/resources/app [main 2025-08-01T12:47:02.256Z] args: { _: [], diff: false, merge: false, add: false, remove: false, goto: false, 'new-window': false, 'reuse-window': false, wait: false, help: false, 'list-extensions': false, 'show-versions': false, 'pre-release': false, 'update-extensions': false, version: false, verbose: true, status: false, 'prof-startup': false, 'no-cached-data': false, 'prof-v8-extensions': false, 'disable-extensions': false, 'disable-lcd-text': false, 'disable-gpu': true, 'disable-chromium-sandbox': false, sandbox: false, telemetry: false, debugRenderer: false, 'enable-smoke-test-driver': false, logExtensionHostCommunication: false, 'skip-release-notes': false, 'skip-welcome': false, 'disable-telemetry': false, 'disable-updates': false, 'use-inmemory-secretstorage': false, 'disable-workspace-trust': false, 'disable-crash-reporter': false, 'crash-reporter-id': '9bc59030-b82d-49d9-9051-898bbb437304', 'skip-add-to-recently-opened': false, 'open-url': false, 'file-write': false, 'file-chmod': false, force: false, 'do-not-sync': false, 'do-not-include-pack-dependencies': false, trace: false, 'trace-memory-infra': false, 'preserve-env': false, 'force-user-env': false, 'force-disable-user-env': false, 'open-devtools': false, 'disable-gpu-sandbox': false, '__enable-file-policy': false, 'enable-coi': false, 'enable-rdp-display-tracking': false, 'disable-layout-restore': false, 'no-proxy-server': false, 'no-sandbox': false, nolazy: false, 'force-renderer-accessibility': false, 'ignore-certificate-errors': false, 'allow-insecure-localhost': false, 'disable-dev-shm-usage': false, 'profile-temp': false, logsPath: '/home/SENSETIME/leisiquan.vendor/.config/Code/logs/20250801T204702' } [main 2025-08-01T12:47:02.258Z] Resolving machine identifier... [main 2025-08-01T12:47:02.262Z] [File Watcher (node.js)] Started watching: '/home/SENSETIME/leisiquan.vendor/.config/Code/User' [49295:0801/204702.266343:WARNING:vaapi_wrapper.cc(1534)] Skipping nVidia device named: nvidia-drm [49295:0801/204702.268231:WARNING:sandbox_linux.cc(420)] InitializeSandbox() called with multiple threads in process gpu-process. [49295:0801/204702.269007:WARNING:viz_main_impl.cc(85)] VizNullHypothesis is disabled (not a warning) [main 2025-08-01T12:47:02.298Z] Resolved machine identifier: 3777bd569b947d43ea914eabe3a61f5de17708a4d53a88839111bf818a20a675 [main 2025-08-01T12:47:02.298Z] Main->SharedProcess#connect [main 2025-08-01T12:47:02.302Z] StorageMainService: creating application storage [main 2025-08-01T12:47:02.306Z] ElectronURLListener: waiting for window to be ready to handle URLs... [main 2025-08-01T12:47:02.306Z] lifecycle (main): phase changed (value: 2) [main 2025-08-01T12:47:02.306Z] windowsManager#open [main 2025-08-01T12:47:02.307Z] windowsManager#open pathsToOpen [ [Object: null prototype] {} ] [main 2025-08-01T12:47:02.307Z] windowsManager#doOpenEmpty { restore: false, remoteAuthority: undefined, filesToOpen: undefined, forceNewWindow: false } [main 2025-08-01T12:47:02.309Z] IPC Object URL: Registered new channel vscode:dc0454f3-919f-4e9a-999c-dc2a526cc059. [main 2025-08-01T12:47:02.309Z] window#validateWindowState: validating window state on 1 display(s) { width: 1200, height: 800, mode: 1, x: 360, y: 140, hasDefaultState: true } [main 2025-08-01T12:47:02.309Z] window#validateWindowState: single monitor working area { x: 72, y: 27, width: 1848, height: 1053 } [main 2025-08-01T12:47:02.309Z] window#ctor: using window state { width: 1200, height: 800, mode: 1, x: 360, y: 140, hasDefaultState: true } [main 2025-08-01T12:47:02.356Z] window#load: attempt to load window (id: 1) [main 2025-08-01T12:47:02.364Z] windowsManager#open used window count 1 (workspacesToOpen: 0, foldersToOpen: 0, emptyToRestore: 0, maybeOpenEmptyWindow: true) [main 2025-08-01T12:47:02.364Z] lifecycle (main): phase changed (value: 3) [main 2025-08-01T12:47:02.364Z] resolveShellEnv(): skipped (VSCODE_CLI is set) [main 2025-08-01T12:47:02.365Z] update#setState idle [main 2025-08-01T12:47:02.407Z] resolveShellEnv(): skipped (VSCODE_CLI is set) [49253:0801/204702.419375:INFO:CONSOLE(3)] "[uncaught exception]: TypeError: Failed to fetch dynamically imported module: vscode-file://vscode-app/usr/share/code/resources/app/out/vs/workbench/workbench.desktop.main.js", source: vscode-file://vscode-app/usr/share/code/resources/app/out/vs/code/electron-browser/workbench/workbench.js (3) [49253:0801/204702.419453:INFO:CONSOLE(3)] "TypeError: Failed to fetch dynamically imported module: vscode-file://vscode-app/usr/share/code/resources/app/out/vs/workbench/workbench.desktop.main.js", source: vscode-file://vscode-app/usr/share/code/resources/app/out/vs/code/electron-browser/workbench/workbench.js (3) [49253:0801/204702.419556:INFO:CONSOLE(4)] "Uncaught (in promise) TypeError: Failed to fetch dynamically imported module: vscode-file://vscode-app/usr/share/code/resources/app/out/vs/workbench/workbench.desktop.main.js", source: vscode-file://vscode-app/usr/share/code/resources/app/out/vs/code/electron-browser/workbench/workbench.js (4) [49253:0801/204702.445759:ERROR:nss_util.cc(227)] Error initializing NSS with a persistent database (sql:/home/SENSETIME/leisiquan.vendor/.pki/nssdb): libsoftokn3.so: 无法打开共享对象文件: 没有那个文件或目录 [49253:0801/204702.445798:ERROR:nss_util.cc(112)] Error initializing NSS without a persistent database: NSS error code: -5925 [49253:0801/204702.445805:FATAL:nss_util.cc(114)] nss_error=-5925, os_error=0 [0801/204702.482445:ERROR:directory_reader_posix.cc(43)] opendir /home/SENSETIME/leisiquan.vendor/.config/Code/Crashpad/attachments/63a4640e-035f-4f73-ac78-2ede9b09a5b9: No such file or directory (2) [49299:0801/204708.716049:ERROR:ssl_client_socket_impl.cc(877)] handshake failed; returned -1, SSL error code 1, net_error -3
最新发布
08-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值