不允许空格的Java脚本验证. [英] Java Script validation for not allowing Spaces.

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

问题描述

我需要验证文本框,而不能在起始位置留空格,并且两个单词之间只能留一个空格..

1234567
例如:Vino Pr --->正确:第5个位置是空格,即只允许一个位置.

123456789
例如:Vino Pr --->此示例在第1位置允许的空间不正确,并且在
之间 允许2个空格..我需要在文本框中获取输入,不允许在第1个位置使用空格,并且在单词之间不允许使用多个空格.

我使用了Text.StartTrim().EndTrim()

从文本框添加到gridview时它正确显示.
但是从Gridview到数据库,它允许有空格.

Hi huys i need to validate the textbox without allowing space starting position and allowing only one space between two words..

1234567
Ex:Vino Pr--->correct: 5th position is space i.e only one position is allowed.

123456789
Ex: Vino Pr--->This Example is incorrect 1st Position allowed Space and in between
allowed 2 spaces.. i need to textbox to get input not allowing the space in the 1st position and not allow the more than one space between the words.

i used the the Text.StartTrim().EndTrim()

it coming correctly while adding in the gridview from textbox.
But from Gridview to database it allowing the spaces.

推荐答案

在这里,我创建了一个JavaScript方法来验证SPACE

第一个位置和最后一个位置不允许有空格,任何单词之间只能有一个空格

此函数将对内容进行Trim()并将检查是否存在空格,如果找到多个空格,则它将提示"NOT VALID",否则"VALID"

Here I have created a JavaScript Method for validating the SPACE

no space allowed in first position and last position, there can be only one space in between any word

This function will Trim() the content and will check the space occurences if more than one space found then it will propmt "NOT VALID" else "VALID"

<script type="text/javascript">

    function AllowSingleSpaceNotInFirstAndLast() {
        var obj = document.getElementById('TextBox1');
        obj.value = obj.value.replace(/^\s+|\s+


/g," "); var CharArray = obj.value.split(" ); 如果(CharArray.length> 2 ){ alert(" ); 返回 ; } 其他 { alert(" ); } 返回 ; } </script></script>
/g, ""); var CharArray = obj.value.split(" "); if (CharArray.length > 2) { alert("User name NOT VALID"); return false; } else { alert("User name VALID"); } return true; } </script></script>







Username: <input type="text" id="TextBox1" name="user" />
<input type="button" value="Submit" onclick="return AllowSingleSpaceNotInFirstAndLast();" />




如果它回答了您的问题,请将其标记为解决方案




Mark it as solution if it answer your question


我认为这段代码对您有帮助
-------------------------------------------------- ------------
i think this code is helpfull to you
--------------------------------------------------------------
<div>
   <script type=""text/javascript">

language="javascript">
       function isNumberKey(evt) {
           //    if (!(((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105)) || (event.keyCode == 8) || (event.keyCode == 9) || (event.keyCode == 37) || (event.keyCode == 39) || (event.keyCode == 46) || (event.keyCode == 190) || (event.keyCode == 35) || (event.keyCode == 36)))

           //event.returnValue=false;

           var charCode = (evt.which) ? evt.which : event.keyCode

           if (charCode > 31 && charCode < 48 || charCode > 57)
               if (event.keyCode != 46)
                   if (event.keyCode != 110)

                       return false;

           return true;



       }


</script>
close the script tag
 
<asp:textbox id="txtAdm" runat="server" onkeypress="return isNumberKey(event)" xmlns:asp="#unknown">
                      Width="150px" MaxLength="8"></asp:textbox>

       </div>


这篇关于不允许空格的Java脚本验证.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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