如果缺少前置文本,则添加文本

时间:2017-08-25 17:14:52

标签: javascript html forms

我正在尝试找到一种方法来添加我们的域名,如果它缺少或没有输入。

以下是我的表单的代码。对于用户名,我希望它检查domainname \是否存在,如果是,那么正常进行,但如果没有domainname \,则添加它。最终结果将是domainname \ username。我尝试在用户名之前执行$('DOMAIN \')。val()+,但这不起作用。

    $(document).ready(function () {
        // Web Proxy request to fetch the configuration
        ajaxWrapper({ url: 'Home/Configuration', dataType: 'xml', success: configSuccess });

        $('form').submit(function () {
            var username = $('#username').val(),
                password = $('#password').val();

            clearMessage();

            if (!username || !password) {
                showMessage('Enter a username and a password');
                return false;
            }

            // Web Proxy request to log the user on
            ajaxWrapper({
                url: 'PostCredentialsAuth/Login',
                dataType: 'xml',
                success: loginSuccess,
                error: loginError,
                data: { username: username, password: password }
            });

            return false;
        });
<form>
        <fieldset>
            <legend>Enter credentials</legend>
            <p>
                <label for="username">User name:</label>
                <input type="text" id="username" name="username" />
            </p>
            <p>
                <label for="password">Password:</label>
                <input type="password" id="password" name="password" />
            </p>
        </fieldset>
        <input type="submit" id="login-button" name="login-button" value="Log On" />
    </form>

1 个答案:

答案 0 :(得分:0)

简单的 indexOf() 检查和可能的字符串连接将起到作用:

&#13;
&#13;
$(document).ready(function () {
      
  // Web Proxy request to fetch the configuration
  ajaxWrapper({ url: 'Home/Configuration', dataType: 'xml', success: configSuccess });

  $('form').submit(function () {
    var username = $('#username').val(),
    password = $('#password').val();

    clearMessage();

    if (!username || !password) {
      showMessage('Enter a username and a password');
      return false;
    }

    // Ensure the user name is correct...
    // If the username has the domain string at position 0, then
    // the username is correct and just use it as normal, but if
    // not, username needs to have the domain prepended.
    // Because of the backslashes in the strings, they need to be 
    // escaped with "\\"
    username = username.indexOf("domain\\") === 0 ? username : "domain\\" + username;

    // Web Proxy request to log the user on
    ajaxWrapper({
      url: 'PostCredentialsAuth/Login',
      dataType: 'xml',
      success: loginSuccess,
      error: loginError,
      data: { username: username, password: password }
    });

    return false;
  });

});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
        <fieldset>
            <legend>Enter credentials</legend>
            <p>
                <label for="username">User name:</label>
                <input type="text" id="username" name="username" />
            </p>
            <p>
                <label for="password">Password:</label>
                <input type="password" id="password" name="password" />
            </p>
        </fieldset>
        <input type="submit" id="login-button" name="login-button" value="Log On" />
    </form>
&#13;
&#13;
&#13;