I wanted to only allow the user to be able to press the submit button if the text in the box has syntax like: City=Detroit
I know that to do this according to text length greater than 0 would work like:
<button type="submit" data-bind="enable: itemToAdd().length > 0">Add</button>
But how would I only allow the user to submit if the text is like %=% ?
Thanks
You may achieve the following behavior by using regular expression matching as shown as below:
<button type="submit" data-bind="enable: /^\w+=\w+$/.test(itemToAdd())">Add</button>
Please refer to the jsfiddle for working example:
http://jsfiddle.net/zeskysee/umrdz96k/
Related
<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
I have this HTML code:
<input type="text" class="input_discount" id="discountCode" placeholder="ENTER YOUR TEXT">
<button type="button" class="btn apply_btn" id="changeType" required/>APPLY</button>
JS:
$('#changeType').click(function(){
$('#discountCode').attr('type','password');
});
The code is working fine. But when I enter the text in the input field and click on apply button, then it converts the input type to password. But when I directly click on apply button and then write inside the input field it shows password format. Can anybody tell me how to prevent this if input field is empty and user clicks on apply button the input type remains focused and should be in text format and after filling the details it should convert to password format.
Why your code was not working.
When there was nothing entered, as per your code it was converting the type to text.
Solution I applied
While the button is clicked, check if textbox is empty or not.
Note: I am considering space as a valid character in the input box, because it can be used for password. If you want to eliminate space you can use
$('#discountCode').val().trim().length
Working Demo
$('#changeType').click(function(e) {
if ($('#discountCode').val().length) {
$('#discountCode').attr('type', 'password');
} else {
$('#discountCode').attr('type', 'text');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="input_discount" id="discountCode" placeholder="ENTER YOUR TEXT">
<button type="button" class="btn apply_btn" id="changeType" required/>APPLY</button>
so i have 2 buttons
<button type="submit" name="print">print</button>
<button type="submit" name="search">search</button>
beside the button search there is a textbox
<input type="text" name="txtSearch" autofocus>
now what i want to do is whenever I press enter i'd like to submit using the button search. but what happens is the button print is always getting submitted because it is on top of button search. I cant put button search on top of print because of the design. Is it possible to do it in php? or do I need javascript? i'm just starting to program and trying to learn more. thank you very much for the help!
You can't do that with php , you should use javascript or jquery .
add an id for your search input like this :
<button id='search' type="submit" name="search">search</button>
then you can use this code in jquery :
$('#search').keydown(function(e) {
var key = e.which;
if (key == 13) {
$('#your-form').submit();
}
});
You can...
Separate into two forms:
<form>
<button type="submit" name="print">print</button>
</form>
<form>
<button type="submit" name="search">search</button>
<input type="text" name="txtSearch" autofocus>
</form>
Or change the print button to:
Print or
<button type="button">print</button>
Or you could put your search button first, and use float or some kind of position:absolute and margin-top CSS.
Or use JavaScript, as Parsa suggests.
I want a button to be disabled unless text is present in a textarea. Here is what I have tried:
<textarea ng-model="shipment_ids"></textarea>
<button ng-click="do_something" ng-disabled="shipment_ids.length"></button>
However, the button is enabled no matter what.
I think you want the opposite of what your code states. Something like:
<textarea ng-model="shipment_ids"></textarea>
<button ng-click="do_something" ng-disabled="!shipment_ids">Click me</button>
Example on JSFiddle here.
I have a form with text input + gradient-shaded button with onclick='this.form.submit()' (plus some hidden inputs).
In all browsers, clicking on the button works.
In Firefox, if I click Enter while in text input, it submits the form.
In IE, it does nothing.
How can I make it work with IE?
If I remember correctly IE likes having an actual submit button, whether that's an <input type="submit" /> or <button type="submit">submit me</button>. Maybe you can put that in there but "out of sight" so you don't see it..
Create an empty text box, with a style of "visibility:hidden;display:none", this is a known issue
This type of form will always work with "Enter":
<form ...>
...
<input type="submit" ...>
</form>
Make sure you have an input of type submit.