Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检查是否';输入字段';包含特定的字母E或B。。。返回所需页面_Javascript_Html_Css_Function_Onclick - Fatal编程技术网

Javascript 检查是否';输入字段';包含特定的字母E或B。。。返回所需页面

Javascript 检查是否';输入字段';包含特定的字母E或B。。。返回所需页面,javascript,html,css,function,onclick,Javascript,Html,Css,Function,Onclick,我正在尝试验证ID为“p_username”的输入字段,如果字符串包含B,则应将其重新转换为“student.html”,如果是E,则应转换为“staff.html”。我的问题在于我不知道.contains的命令是什么。我找到了这个.value,它会发现框中是否只包含给定的字母,但通常这将用于B或E,后跟一个7位字符…因此我需要脚本来检查字符串是否包含B或E 谢谢 <script> function validateUsername() { if (p_username.v

我正在尝试验证ID为“p_username”的输入字段,如果字符串包含B,则应将其重新转换为“student.html”,如果是E,则应转换为“staff.html”。我的问题在于我不知道.contains的命令是什么。我找到了这个.value,它会发现框中是否只包含给定的字母,但通常这将用于B或E,后跟一个7位字符…因此我需要脚本来检查字符串是否包含B或E

谢谢

<script>
function validateUsername() {

    if (p_username.value == "B") {
        location.href = 'student.html';

    } else if (p_username.value == "E") {

        location.href = 'staff.html';
    }
}
</script>

函数validateUsername(){
如果(p_username.value==“B”){
location.href='student.html';
}else if(p_username.value==“E”){
location.href='staff.html';
}
}
像这样试试

if(p_username.value.toLowerCase().indexOf("b")>-1){
  // username contains b
}
像这样试试

if(p_username.value.toLowerCase().indexOf("b")>-1){
  // username contains b
}
请尝试
String.indexOf()

String.indexOf()
返回第一次出现的索引
0
n-1
-1

您也可以根据您的评论尝试
ReqExp.test()

B或E后跟一个7位字符

这里,
^
$
分别是
开始
结束
的锚点
\d
用于数字
0-9
{7}
大小精确到7。

尝试
字符串。indexOf()

String.indexOf()
返回第一次出现的索引
0
n-1
-1

您也可以根据您的评论尝试
ReqExp.test()

B或E后跟一个7位字符


这里,
^
$
分别是
开始
结束
的锚点
\d
对于数字
0-9
{7}
大小精确到7。

基本上您必须使用
索引。请看下面的代码和我为您制作的示例:

$(document).ready(function(){

    $('button').on('click', function(){
        var myValue = $('#myInput').val();
        if(myValue != ''){
            if (myValue.indexOf('B') >= 0) 
            {
                //location.href='student.html';
                alert('contains B');
            } 
            if (myValue.indexOf('E') >= 0 ) {
                alert('contains E');
                //location.href='staff.html'; 
              }
            }
    });

});


我希望它能有所帮助。

基本上你必须使用
索引。请看下面的代码和我为您制作的示例:

$(document).ready(function(){

    $('button').on('click', function(){
        var myValue = $('#myInput').val();
        if(myValue != ''){
            if (myValue.indexOf('B') >= 0) 
            {
                //location.href='student.html';
                alert('contains B');
            } 
            if (myValue.indexOf('E') >= 0 ) {
                alert('contains E');
                //location.href='staff.html'; 
              }
            }
    });

});


我希望这有帮助。

解决了

多亏了Tushar的解算,简单又好,有了.indexOf,但没有解的>-1。只是需要一个原型。谢谢

<script>
function validateUsername() {

if (p_username.value == "")
{
    alert("Please do not leave the input fields blank!");

} else if (p_username.value.indexOf("B") > -1) 
{
    location.href='student.html';

} else if (p_username.value.indexOf("E") > -1) {

    location.href='staff.html'; 
  }
}

</script>

函数validateUsername(){
如果(p_username.value==“”)
{
警告(“请不要将输入字段留空!”);
}else if(p_username.value.indexOf(“B”)>-1)
{
location.href='student.html';
}else if(p_username.value.indexOf(“E”)>-1){
location.href='staff.html';
}
}

已解决

多亏了Tushar的解算,简单又好,有了.indexOf,但没有解的>-1。只是需要一个原型。谢谢

<script>
function validateUsername() {

if (p_username.value == "")
{
    alert("Please do not leave the input fields blank!");

} else if (p_username.value.indexOf("B") > -1) 
{
    location.href='student.html';

} else if (p_username.value.indexOf("E") > -1) {

    location.href='staff.html'; 
  }
}

</script>

函数validateUsername(){
如果(p_username.value==“”)
{
警告(“请不要将输入字段留空!”);
}else if(p_username.value.indexOf(“B”)>-1)
{
location.href='student.html';
}else if(p_username.value.indexOf(“E”)>-1){
location.href='staff.html';
}
}

如果您说B或E始终是第一个字符,那么您可以按如下方式进行检查:

    function validateUsername() 
    {
        if (p_username.value.charAt(0) == "B") 
        {
            location.href = "student.html";
        } else if (p_username.value.charAt(0) == "E") 
        {
            location.href = "staff.html";
        }
    }

如果你说B或E永远是第一个字符,那么你可以检查如下:

    function validateUsername() 
    {
        if (p_username.value.charAt(0) == "B") 
        {
            location.href = "student.html";
        } else if (p_username.value.charAt(0) == "E") 
        {
            location.href = "staff.html";
        }
    }

要检查输入的值是否包含某些字符而不是精确值,请使用
indexOf
作为
if(p\u username.value.indexOf(“B”)>-1){
也只有大写字母或小写字母?请告诉我,这不被视为安全措施,只是为了方便用户?这只是一个原型,不,这不是一个严肃的安全措施。我有一个10个月的项目,第3个月只是测试水域。只是用它作为演示的示例。谢谢@Tushar works非常完美,非常好而且简单。在我没有-1之前,我就有了它。对于原型来说,非常好而且简单。要检查输入的值是否包含一些字符而不是精确值,请使用
indexOf
as
if(p_username.value.indexOf(“B”)>-1){
也只有大写字母或小写字母?请告诉我,这不被视为安全措施,只是为了方便用户?这只是一个原型,不,这不是一个严肃的安全措施。我有一个10个月的项目,第3个月只是测试水域。只是用它作为演示的示例。谢谢@Tushar works非常完美,很好也很简单。在我没有-1之前我就有了它。对于原型来说很好也很简单。