简体   繁体   English

格式化HTML中的错误验证

[英]Formatting The Error Validation in HTML

I am trying to format the text from the error validation JavaScript below: 我正在尝试格式化以下错误验证JavaScript中的文本:

<script>
function validateForm()
{
    var x=document.forms["myForm"]["firstName"].value;
    if (x==null || x=="")
    {
        document.getElementById('usernameError').innerHTML = "Invalid First Name";
        return false;
    }
}
</script>

The JavaScript works as intended, but I need to format the text as it appears on the page. JavaScript按预期工作,但我需要格式化页面上显示的文本。 The following is the username field: 以下是用户名字段:

<fieldset class='step'>
<legend>Personal Information</legend>
<p>
    <label for="firstName">First Name: </label>
    <input type="text" name="firstName"/>
    <div id="usernameError"></div>
</p>
</fieldset>

As you can probably imaging, the JavaScript appends/inserts the text "Invalid first name", if the field is empty, however, I need it to be red (as it is an error) and indented to the right. 由于你可以成像,JavaScript会附加/插入文本“无效的名字”,如果该字段为空,但是,我需要它为红色(因为它是一个错误)并缩进到右边。 How do I achieve that? 我如何实现这一目标?

Add this in css:- 在css中添加: -

#usernameError{
color: red;
font-style: italic;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM