i try to customize the rule of the JQuery validation plugin and I'm almost done. The only problem I got is that the function $("<%=txtUserl.UniqueID %>").val() return undefined. How can I get the value of that textbox ?
$("#aspnetForm").validate
(
{
rules:
{
<%=txtUser.UniqueID %>:
{
required: true,
remote: "CheckUser.aspx?User=" + $("#<%=txtUser.ClientID %>").val()
}
},
messages:
{
<%=txtUser.UniqueID %>:
{
remote: "Invalid user"
}
}
}
);
And in my webform
<asp:TextBox ID="txtUser" runat="server" TabIndex="1" />
UPDATE
I change to use ClientID instead of UniqueID. Also, I put my javascript code at the end of my file instead of in the beginning of the file.
Now, the problem I got is txtUser.val() return an empty string "". in fact, I notice that it's return the old value of the textbox if I change the value. It's doesn't return the current value, which is want I need...
try txtUser.ClientID instead of UniqueID
$("#<%=txtUser.ClientID %>").val()
Note the # that's missing as well.
Yes use the txtUser.ClientID but also you have to use the (#) to get the value of the text box your statement should be
$("#"+"<%=txtUser.UniqueID %>").val()
if you don't use (#) sign then it will give error
hope this will help
Related
I am using Codeigniter 3.x form validation callback method in combination trim and required to validate a field.
The problem is, when I pipe them: trim|required|callback_some_method, the callback method seems to take precedence over trim and required and shows its error message.
Any ideas on this?
EDIT:
This is the rule:
$this->form_validation->set_rules('new_password', 'New Password', 'trim|required|min_length[8]|callback_password_check');
And this is the password_check method:
function password_check($pwd) {
$containsLetterUC = preg_match('/[A-Z]/', $pwd);
$containsLetterLC = preg_match('/[a-z]/', $pwd);
$containsDigit = preg_match('/\d/', $pwd);
$containsSpecial = preg_match('/[^a-zA-Z\d]/', $pwd);
if ( !($containsLetterUC && $containsLetterLC && $containsDigit && $containsSpecial) ) {
$this->form_validation->set_message('password_check', '{field} must contain UPPERCASE and lowercase letters, digits, and special characters.');
return FALSE;
}
return TRUE;
}
The method should return FALSE, but as long as required is before my custom rule and the field is empty, it should stop there with Required field message, NOT the custom method message.
Okay guys, I've managed to solve it by extending the Form_validation library, putting my callback method there and piping as the other rules (without callback_ prefix).
Unfortunately, as described in the code from CI, callbacks validation rules are always verified first, prior to ‘required’ for instance.
There is an official issue opened at CI : https://github.com/bcit-ci/CodeIgniter/issues/5077
I have a simple sign up form with one input tag that is set up for an email. I'm using abide to validate the email. That works, but I would like to create my own error message and to style elements on the page of my choosing. Is this possible?
I found this which I know isn't validating an email input ( I can't seem to find the code that abide is using to validate emails)
abide: {
validators: {
myCustomValidator: function (el, required, parent) {
if (el.value.length <= 3) {
document.getElementById('nameError').innerText = "Name must have more than 3 characters";
return false;
} //other rules can go here
return true;
}
}
I would assume if I am able to set up a custom validator that just mimics what abidie already does (email validation) then I could change all the elements on the page that I wanted to when the validation comes back false.
I'm not sure what exactly you're asking for but checkout the docs http://foundation.zurb.com/sites/docs/abide.html
So it is a little different now in Foundation 6. Once you initiate Foundation, you will have to overwrite certain properties in the Foundation.Abide object. Like this:
Foundation.Abide.defaults.patterns['dashes_only'] = /^[0-9-]*$/;
Foundation.Abide.defaults.validators['myCustomValidator'] = function($el,required,parent) {
if ($el.value.length <= 3) {
document.getElementById('nameError').innerText = "Name must have more than 3 characters";
return false;
} //other rules can go here
return true;
};
Then in your markup in your form would something like this:
<input id="phone" type="text" pattern="dashes_only" required ><span class="form-error">Yo, you had better fill this out.</span>
<input id="max" type="number" data-validator="myCustomValidator" required><span class="form-error">Yo, you had better fill this out.</span>
You can customize the error messages in the span tags.
My teammates and I are learning AngularJS, and are currently trying to do some simple form field validation. We realize there are many ways to do this, and we have tried
putting input through validation filters
using a combination of controller and validating service/factory
a validation directive on the input element
a directive comprising the label, input and error output elements
To me, the directive approach seems the most "correct". With #3, we ran into the issue of having to communicate the validation result to the error element (a span sibling). It's simple enough to do some scope juggling, but it seemed "more correct" to put the span in the directive, too, and bundle the whole form control. We ran into a couple of issue, and I would like the StackOverflow community's input on our solution and/or to clarify any misunderstandings.
var PATTERN_NAME = /^[- A-Za-z]{1,30}$/;
module.directive("inputName", [
function () {
return {
restrict: "E",
require: "ngModel",
scope: {
fieldName: "#",
modelName: "=",
labelName: "#",
focus: "#"
},
template: '<div>' +
'<label for="{{fieldName}}">{{labelName}}</label>' +
'<input type="text" ng-model="modelName" id="{{fieldName}}" name="{{fieldName}}" placeholder="{{labelName}}" x-blur="validateName()" ng-change="validateName()" required>' +
'<span class="inputError" ng-show="errorCode">{{ errorCode | errorMsgFltr }}</span>' +
'</div>',
link: function (scope, elem, attrs, ngModel)
{
var errorCode = "";
if (scope.focus == 'yes') {
// set focus
}
scope.validateName = function () {
if (scope.modelName == undefined || scope.modelName == "" || scope.modelName == null) {
scope.errorCode = 10000;
ngModel.$setValidity("name", false);
} else if (! PATTERN_NAME.test(scope.modelName)) {
scope.errorCode = 10001;
ngModel.$setValidity("name", false);
} else {
scope.errorCode = "";
ngModel.$setValidity("name", true);
}
};
}
};
}
]);
used as
<form novalidate name="addUser">
<x-input-name
label-name="First Name"
field-name="firstName"
ng-model="firstName"
focus="yes"
model-name="user.firstName">
</x-input-name>
<x-input-name
label-name="Last Name"
field-name="lastName"
ng-model="lastName"
model-name="user.lastName">
</x-input-name>
...
</form>
First, because both form and input are overridden by AngularJS directives, we needed access to the ngModel API (ngModelController) to allow the now-nested input to be able to communicate validity to the parent FormController. Thus, we had to require: "ngModel", which becomes the ngModel option to the link function.
Secondly, even though fieldName and ngModel are given the same value, we had to use them separately. The one-way-bound (1WB) fieldName is used as an attribute value. We found that we couldn't use the curly braces in an ngModel directive. Further, we couldn't use a 1WB input with ngModel and we couldn't use a two-way-bound (2WB) input with values that should be static. If we use a single, 2WB input, the model works, but attributes like id and name become the values given to the form control.
Finally, because we are sometimes reusing the directive in the same form (e.g., first name and last name), we had to make attributes like focus parameters to be passed in.
Personally, I would also like to see the onblur and onchange events bound using JavaScript in the link function, but I'm not sure how to access the template markup from within link, especially outside/ignorant of the larger DOM.
I have a field with a proper label that I can fill in with capybara without a problem:
fill_in 'Your name', with: 'John'
I'd like to check the value it has before filling it in and can't figure it out.
If I add after the fill_in the following line:
find_field('Your name').should have_content('John')
That test fails, although the filling just before worked as I've verified by saving the page.
What am I missing?
Another pretty solution would be:
page.should have_field('Your name', with: 'John')
or
expect(page).to have_field('Your name', with: 'John')
respectively.
Also see the reference.
Note: for disabled inputs, you'll need to add the option disabled: true.
You can use an xpath query to check if there's an input element with a particular value (e.g. 'John'):
expect(page).to have_xpath("//input[#value='John']")
See http://www.w3schools.com/xpath/xpath_syntax.asp for more info.
For perhaps a prettier way:
expect(find_field('Your name').value).to eq 'John'
EDIT: Nowadays I'd probably use have_selector
expect(page).to have_selector("input[value='John']")
If you are using the page object pattern(you should be!)
class MyPage < SitePrism::Page
element :my_field, "input#my_id"
def has_secret_value?(value)
my_field.value == value
end
end
my_page = MyPage.new
expect(my_page).to have_secret_value "foo"
If you specifically want to test for a placeholder, use:
page.should have_field("some_field_name", placeholder: "Some Placeholder")
or:
expect(page).to have_field("some_field_name", placeholder: "Some Placeholder")
If you want to test the user-entered value:
page.should have_field("some_field_name", with: "Some Entered Value")
I was wondering how to do something slightly different: I wanted to test whether the field had some value (while making use of Capybara's ability to re-test the matcher until it matches). It turns out that it's possible to use a "filter block" to do this:
expect(page).to have_field("field_name") { |field|
field.value.present?
}
If the field is a hidden one with an id of 'some_field', then you can use
expect(find("input#somefield", :visible => false).value).to eq 'John'
This the easiest way:
expect(page).to have_field("name", with: "your name")
<input type="text" id="name" />
I need to pass string from url:
../page.html?code=123456
to a form (eform snippet in modx)
just once is page loaded (link with url and parameter)
Thanks for answer...
My solution:
1. create a new snippet called GetCode
<?php
if( !function_exists('eformGetCode') ) {
function eformGetCode(&$fields,&$templates){
global $modx;
$code = strip_tags($_GET['codeID']);
$templates['tpl']=str_replace('[+display_code+]',$code,$templates['tpl']);
return true; } }
return '';
?>
2. Add eform call (and snippet) on webpage:
[!GetCode!]
[!eForm? ... ... &eFormOnBeforeFormParse=`eformGetCode` !]
3. In eform chunk with form code add line:
<input name="code" id="code" value="[+display_code+]" eform="::1:" type="text"/>
5. Now when you put parametr in url like:
..../page.html?code=123456
this should appear in form.
you would do this exactly like you would in php..
$myVar = $_GET['code'];
If you are having issues, take a peek in the modx error logs...
-sean
Solution by KudyKam is better than official solution in MODX docs, in which they use a database. http://wiki.modxcms.com/index.php/Populate_eform_with_dynamic_data