Click here to Skip to main content
15,906,626 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
How to display alert message if text box starts with <? or <! or text contains like <text> using java script or jquery.

What I have tried:

Tried using regular expression but its not working properly.
Posted
Updated 4-Apr-16 20:02pm
v4
Comments
Abhipal Singh 5-Apr-16 1:43am    
What does that "" mean.. empty textbox? or a space. Also share the regex that is failing to validate.
Rakesh Tripathy 5-Apr-16 1:59am    
if text box starts with <? or <! or text contains like <text>
Karthik_Mahalingam 5-Apr-16 1:50am    
you mean two double quotes ""??
Rakesh Tripathy 5-Apr-16 1:55am    
if text box starts with <? or <! or text contains like <text>
Rakesh Tripathy 5-Apr-16 2:20am    
how to check for a text contains contains like <text>

1 solution

Try this

HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>

    <script>
        function validateFun() {
            debugger;
            var txt = document.getElementById('txt1');
            var value = txt.value.trim();
            var itemsToValidate = [];
            itemsToValidate.push('<&');
            itemsToValidate.push('<!');
            for (var i = 0; i < itemsToValidate.length; i++) {
                if (value.startsWith(itemsToValidate[i])) {
                    alert('Your message');
                    return false;
                }
            }

            return true;
        }

    </script>
</head>
<body>

    <input type="text" id="txt1" value=" " />
    <button onclick="return validateFun();">sub</button>

</body>



</html>
 
Share this answer
 
Comments
Rakesh Tripathy 5-Apr-16 2:24am    
but how to check for a text contains like <text>
Karthik_Mahalingam 5-Apr-16 2:32am    
starts with ?
Karthik_Mahalingam 5-Apr-16 2:32am    
add this line itemsToValidate.push('<text>');
Rakesh Tripathy 5-Apr-16 2:37am    
if user will input value in text box startwith < and endswith > for e.g: <hello world>
Karthik_Mahalingam 5-Apr-16 2:46am    
give some examples, i cant get it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900