开发者

function call in PHP or JS

Actually I want to validate username nd password only for my os1 == 2 which is linux so please help me

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;
    }

and I have a table like this

<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" 开发者_JAVA技巧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>


I'm not exactly sure what problem you are asking for help with, but here are some observations:

  • Your Javascript is code is trying to get the data from an HTML entity with ID "type", but I don't see such an entity in the code you provided. There is a select box called "os", but nothing called "type". You'd probably get a Javascript error from that.
  • Your password field has two types (password and text)...that's probably not what you are asking about, but it's something that should be fixed.
  • Doing client-side only (without server-side validation) is always a bad idea, since people can just disable Javascript to bypass it.
  • It looks like the code only wants to do validation for sztype == 1, which I assume is Windows, though it's impossible to tell without seeing the contents of $ostype. Do you still need help validating other OS types? How does validation need to differ between OSes?

Sorry for a non-specific answer...it wasn't a terribly specific question.


Try this:

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...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜