ここには多くの誤解があります。
まず第一に、あなたはjQueryプラグインを書いているのではありません。jQueryプラグインは次のように使用されます。
jQuery( ".someSelector" ).myPluginName();
あなたはmytextboxvalidator
、jQuery関数に渡されたメソッドを使用しています$.fn.change
。
第二に、Sparky672が言ったように、フォームの検証には既存のjQueryを使用することを検討する必要があります。そこには、GoogleだけでなくたくさんのjQueryがあります。
第三に、私はいい人なので、プラグインのお手伝いをします。
// myFormValidationPlugin is the name of your plugin, change it as you want
$.fn.myFormValidationPlugin = function( options ) {
// bind the event inside the plugin
this.change(function( ev ) {
// doing some BASIC verification
if( options.pattern.test( $(this).val() ) {
// input is valid
// in your example, I guess it's here you want to return TRUE
}
else {
// input is invalid
// in your example, I guess it's here you want to return FALSE
}
});
// NEVER break the chain
// so yes, you can maintain chainability
return this;
};
// usage
$( ".someSelector" ).myFormValidationPlugin({
pattern: /foo/
});
これがお役に立てば幸いです。ただし、このコードは、を除いては機能しません。それを機能させるには、次のことを行う必要があります。
submit
イベントをフォームのイベントにバインドする
- ハンドラーで、各要素に有効な値があることを確認します
- すべての要素が有効な場合、送信を実行します
- 1つ以上が無効な場合は、たとえば
event.preventDefault
呼び出しでイベントを無効にします