Java脚本文本框问题 [英] java script text box question

查看:71
本文介绍了Java脚本文本框问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi,我想创建一个文本框,该文本框以特殊字符作为输入,它可以同时包含数字和字符,但是应该仅包含一个特殊字符.如果特殊字符计数包括多于1个字符,则应在警告消息后提醒该消息,该消息应保持相同的文本框,但应删除第二次输入的特殊字符.我已经成功编写了代码,因为它只允许使用一个特殊字符,但是在此之后它又显示了第二个特殊字符,我也不想这样做.代码如下所示,请为我的作业提供帮助



hi I want to create a text box which take special character as input and it can take number and character also but it should take only one special character. if the special character count includes more than 1 then it should alert a message after alerting it should maintain the same text box but it should remove the special character which was entered second time. i have succeed in writing code were it will allow only one special character but after that it is displaying the second special character also i don''t want that. the code is show below please help for my assignment



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default7.aspx.cs" Inherits="Default7" %>
<html>
<head>
<script>
    function abc(value) {
        var count = 0;
        var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
        for (var i = 0; i < value.length; i++) {

            if (iChars.indexOf(value.charAt(i)) != -1) {
                count++;

            }
        }

        if (count == 1)
            return true;


        else {

            if (count > 1)
                alert("u cannot enter more than one special charecter");
            Txt1.value = "";
            return false;

        }
    }

</script>
</head>
<body>

<input type="text" id="Txt1" onkeyup="abc(this.value)"/>
<br/>
<input type="button" value="save" id="btn"/>

</body>


</html>

推荐答案

%^& *()+ =-[] \\\';,./{} | \:<> ;? "; for ( var i = 0 ; i < .length; i ++){ 如果(iChars.indexOf( .charAt(i))!= -1){ 数++; } } 如果(计数== 1 ) 返回 其他 { 如果(计数> 1 ) alert(" ); Txt1. = " ; 返回 ; } } </ 脚本 > < /head > < 正文 > < 输入 =" 文本" id Txt1" onkeyup =" abc(this.value)" > < br/ > < 输入 =" 按钮" 保存" id =" btn" > < /body > < /html >
%^&*()+=-[]\\\';,./{}|\":<>?"; for (var i = 0; i < value.length; i++) { if (iChars.indexOf(value.charAt(i)) != -1) { count++; } } if (count == 1) return true; else { if (count > 1) alert("u cannot enter more than one special charecter"); Txt1.value = ""; return false; } } </script> </head> <body> <input type="text" id="Txt1" onkeyup="abc(this.value)"/> <br/> <input type="button" value="save" id="btn"/> </body> </html>


我已经修改了您的代码,请检查是否对您有帮助...

I''ve modified your code, check if this help you...

function abc(value) {
            var count = 0;
            var iChars = "!@#


%^& *()+ =-[] \\\';,./{} | \:<>?" ; var text = value; var array = value.split(" ); for ( var i = 0 ; i < array.length; i ++){ 如果(iChars.indexOf(array [i])!=- 1 ){ 数++; 如果(计数== 2 ){ 文字= value.substr( 0 ,value.lastIndexOf(array [i])); break ; } } } 如果(计数== 2 ){ alert(" ); } 文档 .getElementById(" ) .value =文字; }
%^&*()+=-[]\\\';,./{}|\":<>?"; var text = value; var array = value.split(""); for (var i = 0; i < array.length; i++ ) { if (iChars.indexOf(array[i]) != - 1) { count++; if (count == 2) { text = value.substr(0, value.lastIndexOf(array[i])); break; } } } if (count == 2) { alert("u cannot enter more than one special charecter"); } document.getElementById("Txt1").value = text; }


这篇关于Java脚本文本框问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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