react 下的表單驗證元件Formsy
阿新 • • 發佈:2019-02-11
github上有比較詳細的介紹點選開啟連結,這裡只是記錄下
基本格式就是這樣,現有的一般不會直接submit,onValidSubmit--校驗成功的處理 onInvalidSubmit--校驗失敗的處理,這裡我覺得基本的比如自定義的正則,長度,required,email都可以,如果是需要後臺做判別重複的,就和上一個jquery的validation一樣,處理的不好,這裡就是自己寫input,不用MyInput去處理,看電子郵箱那裡,還是用onInput去處理了(別人的程式碼,其實如果只是處理email格式,MyInput是可以用valitions="isEmail"來處理的
)
{typeof window !== 'undefined' && ( <email的處理Formsy.Form name='T_fea0' ref="form" onValidSubmit={this.submitOk} onInvalidSubmit={this.submitFalse}> <div name='T_6cb1' className="clearfix line"> <div name='T_1e48' className="inputs"> <MyInput required value={this.state.authInfo.fullName ? this.state.authInfo.fullName : ''} name="fullName" maxLength="100" title="企業全稱:" /> </div> </div> <div name='T_778f' className="clearfix line"> <div name='T_9c61' className="inputs"> <MyInput required value={this.state.authInfo.locationCountryName ? this.state.authInfo.locationCountryName + '(' + this.state.authInfo.locationCountryCode + ')' : ''} readonly ref="locationCountryCode" name="locationCountryCode" title="所在地:" id="country" /> </div> </div> <div name='T_80d7' className="clearfix line" style={{display: this.state.countryCode || this.state.authInfo.locationCountryCode ? 'block':'none'}}> <div name='T_92c7' className="inputs not-required"> <MyInput required value={this.state.authInfo.locationCityName ? this.state.authInfo.locationCityName : ''} readonly ref="locationCityCode" name="locationCityCode" id="city" /> </div> </div> <div name='T_8bdb' className="clearfix line"> <div name='T_5549' className="inputs"> <MyInput required value={this.state.authInfo.residence ? this.state.authInfo.residence : ''} maxLength="30" name="residence" title="住所:" /> </div> </div> <div name='T_f410' className="clearfix line"> <div name='T_d110' className="inputs"> <div name='T_7bb7' className={this.state.emailError ? 'form-group error' : 'form-group'}> <label name='T_1e57' for="email" className="required"> 電子郵箱: </label> <input name='T_fb82' type="text" value={this.state.email || this.state.authInfo.email} onInput={this.validEmail}maxLength="30" /> <span name='T_5958' className="validation-error" style={{display: this.state.emailError ? 'block':'none'}}>{this.state.emailErrorTxt} </span> </div> </div> </div> <div name='T_c67b' className="clearfix line"> <div name='T_5666' className="inputs"> <MyInput required value={this.state.authInfo.telNo ? this.state.authInfo.telNo : ''} name="telNo" maxLength="30" validations={{ matchRegexp: /^([\d\s\-\+\(\)]+)+$/ }} validationError="請輸入數字、空格和符號()-+" title="公司電話:" /> </div> </div> <div name='T_e3df' className="clearfix line"> <div name='T_ab97' className="inputs not-required"> <MyInput value={this.state.authInfo.webUrl ? this.state.authInfo.webUrl : ''} name="webUrl" maxLength="140" validations={{ maxLength: 140 }} validationError="最長輸入140個字元" title="網址:" /> </div> </div> <hr name='T_b5b0' /> <div name='T_f3b7' className="clearfix line"> <div name='T_a036' className="inputs word-wrap"> <MyInput required value={this.state.authInfo.taxCode ? this.state.authInfo.taxCode : ''} name="taxCode" maxLength="30" validations={{ matchRegexp: /^[\w\-]+$/ }} validationError="請輸入大小寫英文、數字和符號“-”、“_”" title="統一社會信用程式碼(稅號):" /> </div> </div> <div name='T_6570' className="clearfix line"> <div name='T_47ef' className="inputs"> <MyInput required value={this.state.authInfo.legalPerson ? this.state.authInfo.legalPerson : ''} name="legalPerson" maxLength="30" title="法人代表:" /> </div> </div> <div name='T_7ace' className="clearfix line"> <div name='T_bb41' className="inputs"> <div name='T_2697' className="form-group"> <label name='T_21a2' for="license" className="required"> 營業執照: </label> <div name='T_80ce' id="m-upload-more" className="m-upload m-upload-multiple clearfix"> <div name='T_ff03' className="upload-img-box clearfix" /> <div name='T_52c9' className="upload-btn-box"> <input name='T_bbb3' multiple="multiple" type="file" accept="image/jpeg,image/png,image/bmp,image/tiff,application/pdf" className="upload-input-file" /> </div> </div> <span name='T_013c' style={{color: this.state.imgError ? '#eb4b51':'#999999'}}>{this.state.imgErrorTxt}</span> </div> </div> </div> <hr name='T_0921' /> <div name='T_99b9' className="clearfix line"> <div name='T_c4f5' className="inputs not-required"> <MyInput value={this.state.authInfo.depositBank ? this.state.authInfo.depositBank : ''} name="depositBank" maxLength="30" title="開戶銀行:" placeholder="XX銀行XX支行(分行)" /> </div> </div> <div name='T_dcf6' className="clearfix line"> <div name='T_560a' className="inputs not-required"> <MyInput value={this.state.authInfo.bankAccountNo ? this.state.authInfo.bankAccountNo : ''} name="bankAccountNo" maxLength="30" validations={{ matchRegexp: /^[a-zA-Z0-9]+$/ }} validationErrors={{ matchRegexp: '請輸入字母或數字' }} title="銀行賬號:" /> </div> </div> <div name='T_7eb8' className="clearfix line"> <div name='T_83e3' className="inputs"> <MyInput required value={this.state.authInfo.currencyName ? this.state.authInfo.currencyName + '(' + this.state.authInfo.tradingCurrency + ')' : ''} ref="tradingCurrency" name="tradingCurrency" title="交易幣種:" id="currency" /> </div> </div> <div name='T_f006' className="clearfix line"> <div name='T_3998' className="inputs"> <MyInput required value={this.state.authInfo.settlementName ? this.state.authInfo.settlementName : ''} ref="refSettlementId" name="refSettlementId" title="結算方式:" id="clearing" /> <span name='T_63dc' className="inputs-tip">需要您和我方溝通確認具體的結算方式</span> </div> </div> {/* <div name='T_95c5' className="clearfix line"> <div name='T_b3f7' className="inputs"> <MyInput required value={this.state.authInfo.businessTypeValue ? this.state.authInfo.businessTypeValue.replace(/\,/g, '/') : ''} ref="businessType" name="businessType" title="業務型別:" id="business" /> <span name='T_a34d' className="inputs-tip">選項為您可能用到的業務型別,可多選</span> </div> </div> */} <div name='T_de94' className="clearfix line"> <div name='T_eae3' className="inputs not-required"> <div name='T_dcb5' className="form-group"> <label name='T_796d' for="mainBusiness">主營業務:</label> <InputComponent type="textarea" value={this.state.authInfo.mainBusiness ? this.state.authInfo.mainBusiness : ''} rows="4" cols="40" maxLength="140" handleText={this.showResult} name="mainBusiness" /> </div> </div> </div> <div name='T_a0df' className="clearfix submit"> <button name='T_7529' type="submit">提交資訊</button> </div> </Formsy.Form> )}
validEmail: function(e) { var value = e.target.value; if (value === '') { this.setState({ emailError: false }); this.setState({ email: value }); return; } var _this = this; this.setState({ email: value }); if (!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(value)) { this.setState({ emailError: true, emailErrorTxt: '電子郵箱格式不正確' }); return; } else { ent_authService.checkEmail({ email: value }, function(res) { if (res.errorCode !== 0) { _this.setState({ emailError: true, emailErrorTxt: '電子郵箱已存在' }); } else { _this.setState({ emailError: false }); } }); } },這裡的onValidSubmit也是要特別再處理MyInput處理不了的情況