1

I wrote a function in which I pass, mainly, a regex and an error message. What I'd like to achieve is that the browser reads the HTML code inside the Javascript code as HTML, not as a string. I've read many posts here on Stack but people seem to have exactly the opposite problem.

oneRegex(/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9]).{8,}$/, inputValue, 'Password must contain, at least:<br> <ul><li>one number</li> <li>one capital letter</li> <li>one lower case letter</li> <li>8 carachters long</li></ul><br> i.e: passWord3', inputID);

In this case I'd like a result like:

Password must contain, at least:

  • one number
  • one capital letter
  • one lower case letter
  • 8 carachters long

Javascript functions

function addErr(errMsg, inputID) {
    $('#subButton').prop('disabled', true);
    $('#err' + inputID).fadeIn(500).text(errMsg);
    $('#err' + inputID).css('background-color', '#E3BFC0');
    $('#' + inputID).css('background-color', '#E3BFC0');
}
function removeErr(inputID) {
    $('#subButton').prop('disabled', false);
    $('#err' + inputID).fadeOut(500);
    $('#' + inputID).css('background-color', 'inherit');
}

function oneRegex (regex, inputValue, errMsg, inputID) {
    if (!regex.test(inputValue)) {
        addErr(errMsg, inputID);
    }
    else {
        removeErr(inputID);
    }
}
2
  • 1
    you want the html interpreter to format the text use innerHTML instead of textContetn. youre not showing the code that acctually puts it in the dom so i'm not sure how you expect others to debug... Commented Dec 21, 2016 at 15:02
  • @Iwrestledabearonce. I edited the question; I should use .html() instead of .text() then? Commented Dec 21, 2016 at 15:05

1 Answer 1

3

Just replace .text with .html method, like this:

$('#err' + inputID).fadeIn(500).html(errMsg);
Sign up to request clarification or add additional context in comments.

1 Comment

Exactly, I tried on @Iwrestledabearonce. suggestion and it perfectly works! Thanks Kirill!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.