Ways to prevent survey respondents from inputting spaces as their first character - javascript

I am basically new to html/css/javascript and I just got thrown a problematic issue.
I have a text box for my "Others, please specify" option. However, there are instances where some respondents can proceed to the next question by just entering a space and nothing else. When I download my data, the field became blank and hence more cleaning work is needed.
Just wondering if there any ways (html, css or javascript) that I can make use of to prevent my respondents from entering just spaces for the very first character. Any help would be appreciated. Thank you.

Put the required input in a form with a pattern which requires non-space characters:
<form>
<input required pattern="\S.*">
<button>submit</button>
</form>
\S.* means - match one non-space character (at the beginning of the string), followed by anything.

Consider using RegEx and use your desired pattern. In your case /[^\s]/ (matches anything that is not a whitespace character) will do.

Related

HTML input element password pattern with alphabet and number range

I know this may be very simple but I couldn't find anything relevant. I am using the HTML element for accepting the password from the user.
There is a condition for the password to be accepted
It should only contain letters between a to h (0 times or more)
It should only contain numbers between 1 to 8 (0 times or more)
Following the above two conditions the user can come up with any password combination. For example: abc123, 6ad27, hefb etc etc which should be accepted by the input element.
But it should not accept patterns like z00911, ksoql234 etc.
What should be the value for pattern attribute in the following code snippet for checking the above two conditions?
<input type="password" pattern="WHAT-SHOULD-I-PUT-HERE">
I hope someone might help. Thank you
<form action="/blablabla">
<input type="password" pattern="[a-h1-8]+" required="required" title="Wrong password">
<input type="submit">
</form>
In regular expression [a-h] means range of character, you can define multiple ranges in square brackets: [a-h1-8]. If you want to allow repetitions of pattern you add *(0 or more repetitions) or +(1 or more repetition) after pattern. Your pattern for single letter is [a-h1-8] so for password containing at least on character full pattern is [a-h1-8]+. You can read more here.
I have also added required attribute to enforce filling password field, without that attribute user could simply leave password blank.
You can handle this problem easily with javascript functions such as onChange event on your input tag.
Every onChange event you can check the last added character or letter whether meets your conditions.

I want to limit user to enter space after text in jquery and will remove the space if it after text?

Below's code will replace space with '-', so if the user input a lot of space after text, it will be like 'apple---------'. Therefore, I want to limit user enter one space after the text, it will be apple- and will not accept anymore space but only text, e.g. apple-red but not apple--- or apple--red.
if the user submit the text and if space after the text, I want to know how to remove the space either? Thanks!
var $input = $('#tag');
$input.val(function(_,v){
return v.replace(/\s+/g,'-');
});// '-'repace space
<input type="text" id="tag" />
http://www.w3schools.com/jsref/jsref_trim_string.asp
This would be a lot easier, and my gut says a lot faster too. Use trim, then add your "-" if you really need it.

Set letter spacing of last character in input field

I'm currently creating an input field that requires letter spacing, but if the last letter is spaced it shifts everything out of alignment... I was just wonder if there would be anyway of finding the last character of the input field which will be the 13th character with jquery or JavaScript and setting the letter spacing to 0.
Any help appreciated.
Check out Lettering.js. It wraps characters in HTML, allowing you to manipulate them more finely with CSS.
If I understand you correctly, you are trying to trim the trailing space right??
If so you can simply trim the input field value every time it changes. Here is the JQuery solution.
$.trim(MyInput.value);

PHONE VALIDATION: How To Overwrite Text In a Textbox When Typing Without Clearing Whole Field?

I would like to know how to create a script that overwrites text that is already in a text box. (in jquery or javascript)
Example: If I have a text phone field that says:
+1 (xxx) xxx-xxxx
When a user clicks the field, I want the characters to remain, and the focus set to the 4TH character in the text box, just after the 1.
Then, as a user types each number, it overwrites the x one by one, until all the x's are gone. But, I want the parenthesis and hyphen formatting to stay, so the users input forms around the formatting.
I would also like this form to only allow numbers, hyphens, and parenthesis, and not allow submitting if x's still exist.
If you can help me with this, THANK YOU! :-)
Try masked input plugin # [
http://digitalbush.com/projects/masked-input-plugin/
]1
Looks like it matches what you need.

Allow only certain characters

I'd like to block all characters from being inputed except 0-9,a-z,A-Z range only alphanumeric characters. So when someone types ! for examplee nothing is written into input. How can I do that?
You need to write a function that listens for the onkeypress event for the form field, then check to see if the form contains any unwanted characters, and if it does, you update the field with those characters removed.
Or maybe you can use Alphanumeric Plugin for jQuery :
$('#yourInput').alphanum();

Categories

Resources