Javascript提示()输入验证 [英] Javascript Prompt() Input validation

查看:72
本文介绍了Javascript提示()输入验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力使用HTML代码来验证外部js文件中Javascript提示符()的输入。我知道调用Javascript函数来验证,我知道如何编写函数,但是如何听提示符并在HTML中输入?



提示必须在HTML文档中以表单的形式完成吗?

解决方案

对不起,是我在上面的评论中提到的代码。 jsFiddle 现场示例



这是一个完整的工作,独立的例子。注意,你必须在代码本身之前引用/加载jQuery和jQueryUI库(在标签中完成)。

/ p>

老实说,对于网站来说,prompt()对于这些日子来说是个坏主意。也许这更像你想做的事情。验证(用户输入的内容)在对话框的关闭部分完成。

 < HTML> 
< head>
< script src =// ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js\"> ;</script>
< script src =// ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js\"> ;</script>
< link rel =stylesheethref =http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css/>

< script type =text / javascript>
$(document).ready(function(){
$ b $('#mybutt')。click(function(){
$('#msg')。dialog ({
autoOpen:true,
modal:true,
title:'This is a question:',
buttons:{
Go:function(){
$(this).dialog('close');
}
},
close:function(){
var s = $('#sumpin') .val();
if(s.length <15)
alert('请输入超过15个字符');
else
alert('You typed:'+ s);
}
});
});

}); // END $(document).ready()

< / script>
< / head>
< body>

< div id =msgstyle =display:none>
在此处输入内容:< br />
< input id =sumpintype =text>
< / div>
< br />
< input type =buttonid =mybuttvalue =Click Me>

< / body>
< / html>


I'm struggling with the HTML code to validate the input of a Javascript prompt() in an external js file. I understand calling the Javascript function to validate and I know how to write the function, but how do you "listen" for the prompt and subsequent input in HTML?

Does the prompt have to be done in the HTML doc as a form?

解决方案

Sorry, I can't wait around... Here is the code that I mentioned in my comment above. jsFiddle live example here.

This is a fully working, stand-alone example. Just copy/paste into a file and run.

Note that you must reference / load the jQuery and jQueryUI libraries before the code itself (done here in the tags).

Honestly, prompt() is a bad idea for websites these days. Perhaps this is more like what you want to do. Validation (of what the user typed) is done in the close section of the dialog.

<html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
        <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />

        <script type="text/javascript">
            $(document).ready(function() {

                $('#mybutt').click(function() {
                    $('#msg').dialog({
                        autoOpen:true,
                        modal:true,
                        title: 'This is a question:',
                        buttons: {
                            Go: function() {
                                $(this).dialog('close');
                            }
                        },
                        close: function() {
                            var s = $('#sumpin').val();
                            if (s.length<15)
                                alert('Please type more than 15 characters');
                            else
                                alert('You typed: ' + s);
                        }
                    });
                });

            }); //END $(document).ready()

        </script>
    </head>
<body>

    <div id="msg" style="display:none">
        Type something here:<br />
        <input id="sumpin" type="text">
    </div>
    <br />
    <input type="button" id="mybutt" value="Click Me">

</body>
</html>

这篇关于Javascript提示()输入验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆