To prevent only spaces in textarea - javascript

How do I prevent only spaces in the text area? I don't want to allow only white spaces in the text area using HTML pattern or Validator.pattern.

You can use this regex for no spaces:
<form>
<input type="text" pattern="[^' ']+" />
</form>

Related

Disable submit button whenever there is a white space in input tag

<form name="regForm">
<input type="text" id="username" name="username" ng-model="username" required>
<button ng-click="submitSignup()" type="submit" ng-disabled=" (regForm.username.$dirty && regForm.username.$invalid) || regForm.username.$pristine">Sign Up</button>
</form>
I want the input field to be invalid and the sign up button disabled whenever one presses white space. I don't want to allow white space in the input field either by typing or by pasting. The sign up button should be disabled whenever there is a white space either by typing or by pasting. Can someone help me out on this?
the easy way to validate white spaces, is to trim the value of the input.
if(regForm.username.trim())
This conditional is true is the result is different to empty string.
Regards.
you can also use ng-pattern to validate the input and disable the button on invalid pattern.
Follow that link

how to line break on placholder of text area [duplicate]

This question already has answers here:
Insert line break inside placeholder attribute of a textarea?
(25 answers)
Closed 4 years ago.
I am looking to line break on placeholder of textarea on reactjs
So far I have tried to use \n, <br>, nl2br, \\n and none of them worked
<Input type="textarea" name="txtarea" placeholder="Line1 \n Line2" />
The current output im getting all in the same line as Line1 \n Line2
My goal is to have them out on a separate line as
Line1
Line2
Newlines do not work in the placeholder attribute of <input> elements, but they work for the <textarea> element.
However, using <input type="textarea"> is not valid markup and will be replaced by the browser to an <input type="text">.
If you want multi-line input, use a <textarea> instead.
For the newline, use the 
 and
HTML entities in your placeholder, which are the line feed and the new line characters:
<input type="text" placeholder="line
line2">
<textarea placeholder="line1
line2"></textarea>
You have to put that
insted \n inside the placeholder

Angular JS: preserve leading zeros for a number

I want to preserve the leading zeros for a number but as usual it trims them.
I tried
<input type="tel" pattern="[0-9]*">
But this allows text input which breaks my case.
Please help!!!
You can use the input type="text", use the ng-pattern to validate your expressions and then use ng-message to validate your field and display errors.
This won't remove your lead 0s.
So your HTML will look something like this:
<input type="text" ng-pattern="[0-9]*" ng-model="myTel">
<div ng-messages="myForm.myTel.$error">
<div ng-message="pattern">You can insert only digits here</div>
</div>

white space regex in javascript

I have written a HTML 5 file. In form I have one required text field. I want to validate this text field in such way that user can start the value in it with a space. But he is not allowed to enter only a space character. Means, he can start typing with a space but must not enter only a space character in text field. To work around this, I gave a pattern attribute through javascript's setAttribute method.
<form method="post" action="" id="validation">
Name:<input type="text" id="nome" name="nome" required="required" oninvalid="this.setCustomValidity('Name is required field')" oninput="setCustomValidity('')" /><br />
<br><br>
<input type="submit" value="Enviar" />
</form>
<script>
document.getElementById('nome').setAttribute("pattern",".*\S+.*");
</script>
output: when I gave only space. It works. But when I start typing with a space and then some characters, then It again validates: "Name is required field.", whereas now it should mark it correct.
one more thing, I want to add the pattern attribute through javascript only.
How can I resolve this problem in such a way that when user can start typing with a space but should not be allowed to enter only a space.
The minimal pattern you need is
^ *[^ ]+.*$
If you want to ask for exact non-space characters in the middle (for example, 5 or more) of a string then use
^ *[^ ]{5,}.*$
Notice that the above patterns are indifferent to spaces in the end of matching strings, and if you want to forbid trailing spaces then remove the .* part.
Also if nessesary you may change the space character to \s everywnere in these examples to catch also tabs, carriage return, form feed etc. characters.
It seems you simply want to prevent the user from entering only space characters. You could try simply trimming an instance of the input. It would be more reliable than using a RegEx for this scenario.
If the trimmed instance comes to have null length, then the user entered only space characters. Then you can return an "invalid" error.
Here is an example of the logic:
if(string.trim().length === 0) {
return false;
}
Here is a fiddle: http://jsfiddle.net/esvsLx8u/

Restricting the format of an input tag

Within an <input type="text" /> tag how do you I mask the input field so that the user is resricted to the following format [].[] and in between the brackets can be any number of characters.
As an example:
"[Analysis].[Analysis]"
or a another example:
[Analysis-Result].[Analysis Result]"
Use this plugin http://digitalbush.com/projects/masked-input-plugin/#demo

Categories

Resources