PHP或JS中的函数调用

时间:2011-04-26 04:48:19

标签: php javascript

实际上我只想为我的os1 == 2 linux验证用户名和密码,请帮助我

if (os1 == 1) os = "Windows";

if (os1 == 2) os = "Linux";
if (os1 == 3) os = "Others";


function Validate() {

var dropdownIndex = document.getElementById('type').selectedIndex;
var sztype = document.getElementById('type')[dropdownIndex].value;

if (sztype == "1") {
    var szpassword = "";
    var szguid = "";
    var name = Trim(document.userform.name.value);

    var ip = Trim(document.userform.ip.value);
    if (ip.length == 0) {
        alert("Please specify IP/Host Name.");
        return false;
    }
    if (name.length == 0) {
        name = ip;
    }

    szusername = Trim(document.userform.username.value);

    if (szusername.length == 0) {
        alert("Please specify Username.");
        return false;
    }

    var szvalidchars = new RegExp("[~!@#$%^&*()+|{}:\"<>?,/;'=\\`]");
    if (szvalidchars.test(szusername)) {
        alert("Username cannot contain invalid characters.");
        return false;
    }

    szpassword = Trim(document.userform.password.value);
    if (szpassword.length == 0) {
        alert("Please specify Password.");
        return false;
    }

我有一张这样的表

<form name="userform">
<table cellpadding="10" cellspacing="0" border="0" width="100%">
<tr>
    <td><table width="100%" cellpadding="0" cellspacing="0">
        <tr>
          <td style="padding-left:12px; padding-top:10px;  font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;">To add single/multipe physical server(s), enter the IP address/range of physical server(s).</td>
        </tr>
      </table></td>
  </tr>
    <tr>
    <td></br>
    <table border=0> 
    <tr>
        <td style="padding-left:20px; padding-top:5px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;">Type: </td>
        <td style="padding-top:5px; padding-left:50px;">
        <select name="type" id="type" class="SelectList Width200" onchange="OnCbTpyeChange()">
        <?php
        foreach($type as $key => $value)
        {
            echo  "<option value=\"".$key."\"";
            echo">";
            echo $value;
            echo "</option>";
        }
        ?>
        </select>
        </td>
    </tr>
    </table>
    <table id="hostname_table" name="hostname_table" border=0>
    <tr>
        <td style="padding-left:20px; padding-top:10px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;">Name: </td>
        <td style="padding-top:10px;">
        <input name="name" type="text" maxlength="50" >
        </td>
    </tr>
    <tr>
        <td style="padding-left:20px; padding-top:10px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;">IP/Host Name: </td>
        <td style="padding-top:10px;">
        <input name="ip" type="text" maxlength="50" >
        </td>
    </tr>
    <tr>
        <td style="padding-left:20px; padding-top:10px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;">User Name: </td>
        <td style="padding-top:10px;">
        <input name="username" type="text" maxlength="50" >
        </td>

    <tr>
        <td style="padding-left:20px; padding-top:10px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;">Password: </td>
        <td style="padding-top:10px;">
        <input type="password" name="password" type="text" maxlength="50">
        </td>
    </tr>
    <tr>
        <td style="padding-left:20px; padding-top:10px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;">OS: </td>
        <td style="padding-top:10px;">
        <select name="os" id="os" class="SelectList Width200">
        <?php
        foreach($ostype as $key => $value)
        {
            echo  "<option value=\"".$key."\"";

            echo">";
            echo $value;
            echo "</option>";
        }
        ?>
        </select>
        </td>
    </tr>
    </table>
    <table id="nwrange_table" name="nwrange_table" style="display:none">

        <tr>
            <td style="padding-left:20px; padding-top:10px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;">Start IP Address: </td>
            <td style="padding-top:10px;">
            <input name="startip" type="text" maxlength="50" id='startip'>
            </td>
        </tr>
        <tr>
            <td style="padding-left:20px; padding-top:10px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;">End IP Address: </td>
            <td style="padding-top:10px;">
            <input name="endip" type="text" maxlength="50" id='endip'>
            </td>
        </tr>
    </table>


    </td>
    </tr>

                                                                  

2 个答案:

答案 0 :(得分:1)

我不是确切地确定你要求帮助的问题,但是这里有一些观察:

  • 您的Javascript代码正在尝试从ID为“type”的HTML实体获取数据,但我在您提供的代码中看不到这样的实体。有一个名为“os”的选择框,但没有任何名为“type”的框。您可能会从中获得Javascript错误。
  • 您的密码字段有两种类型(密码和文字)...这可能不是您所询问的内容,但它应该被修复。
  • 只做客户端(没有服务器端验证)总是一个坏主意,因为人们只能禁用Javascript来绕过它。
  • 看起来代码只想对sztype == 1进行验证,我认为是Windows,但是如果没有看到$ostype的内容就无法判断。您是否仍需要帮助验证其他操作系统类型?如何验证操作系统之间的差异?

对于非特定答案感到抱歉......这不是一个非常具体的问题。

答案 1 :(得分:0)

试试这个:

function Validate()
{
    var dropdownIndex = document.getElementById('type').selectedIndex;
    var sztype = document.getElementById('type')[dropdownIndex].value;

    if(sztype == "1")
    {
        //var os1 = userform.os.selectedIndex;
        var dropdownosIndex = document.getElementById('os').selectedIndex;
        var os1 = document.getElementById('os')[dropdownosIndex].value;

        if (os1 == 1)
            os = "Windows";

        if (os1 == 2)
            os = "Linux";

        if (os1 == 3)
            os = "Others";

        var ip = Trim(document.userform.ip.value);
        szusername = Trim(document.userform.username.value);
        szpassword = Trim(document.userform.password.value);

        if(os != 'Linux')
        {
            if(ip.length == 0)
            {
                alert("Please specify IP/Host Name.");
                return false;
            }
            if(name.length == 0)
            {
                name=ip;
            }

            if(szusername.length == 0)
            {
                alert("Please specify Username.");
                return false;
            }

            var szvalidchars = new RegExp("[~!@#$%^&*()+|{}:\"<>?,/;'=\\`]");
            if(szvalidchars.test(szusername))
            {
                alert("Username cannot contain invalid characters.");
                return false;
            }

            if(szpassword.length == 0)
            {
                alert("Please specify Password.");
                return false;
            }
        }

        var szpassword = "";
        var szguid = "";
        var name = Trim(document.userform.name.value);

        var hostid = "Physical";
        szHostName = hostid;
        szname = name;
        var os = "Others";

        params = "guid="+szguid+"&username="+szusername+"&password="+szpassword+"&ip="+ip+"&name="+name+"&os="+os ;
        //alert(params);
        document.body.style.cursor = 'wait';//change cursor to wait

        if(!http)
            http = CreateObject();

        nocache = Math.random();

        http.open('post', 'addvm.php');
        http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        http.setRequestHeader("Content-length", params.length);
        http.setRequestHeader("Connection", "close");
        http.onreadystatechange = SaveReply;

        http.send(params);
    }

    // Etc...