0

I've write css in textarea but need to validate that css which I'm writing it's properties and value is correct or not ?

I've to develop a feature like real time css change along with validating that css For instance if I write .abc { text : text; } then this text is not a valid property nor valid css so I have to show notification/error in textarea itself like : This text is not a valid css

Html Code :

<textarea id="txtCSS" name="txtCSS" rows="14" cols="50">
    .FC{color:blue; margin: 40px; padding: 50px;}
    .cf{color:blue; margin: 40px; padding: 50px; }
</textarea>
<label class="FC">Some Text</label>
<div class="cf">test Text</div>

JavaScript Code :

function render(value) {
        let previewStyle = document.getElementById('previewStyle');
        if (!previewStyle) {
            addStyle(value);
        } else {
            previewStyle.innerText = value;
        }
    }
    function addStyle(styles) {
        /* Create style document */
        const css = document.createElement('style');
        css.type = 'text/css';
        css.id = 'previewStyle';
        if (css.styleSheet) {
            css.styleSheet.cssText = styles;
        } else {
            css.appendChild(document.createTextNode(styles));
        }
        /* Append style to the tag name */
        document.getElementsByTagName("head")[0].appendChild(css);
    }
    const text = document.getElementById('txtCSS');
    text.addEventListener('input', (e) => {
        const content = e.target.value;
        render(content);
    });
    render(text.value);

Referance Image :

enter image description here

4
  • Note: there's a difference between "syntax valid" css and css that's "valid". eg add body {display:none;} to your textarea... you might like to rethink your approach: add an iframe and change the css in that iframe so that it doesn't affect the page that you're using to input the values. Commented Mar 10, 2022 at 12:39
  • Previously asked here (though in 2010, so may be a newer solution) - quick google and most suggestions are, like that answer, to use an existing validator, either online or download. Commented Mar 10, 2022 at 12:41
  • @freedomn-m is this will be used in my existing project as I've to develop a feature like real time css change along with validating that css For instance if I write .abc { text : text; } then this text is not a valid property nor valid css so I have to show notification/error in textarea itself like : This text is not a valid css Commented Mar 10, 2022 at 12:50
  • 1
    Does this answer your question? Format and Validate CSS with jquery Commented Mar 10, 2022 at 15:02

1 Answer 1

2

You can try like below It should work

   const validatorURL = 'https://jigsaw.w3.org/css-validator/validator' +
  '?uri=http%3A%2F%2Fwww.w3.org%2F&profile=css3&output=json';
fetch(validatorURL)
  .then(response => response.json())
  .then(results => console.log(results))

Sign up to request clarification or add additional context in comments.

1 Comment

Yes this is working for me For Refernce : jsfiddle.net/Kaushal_Joshi/gfu8aL9j/17

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.