wrest.js에 숫자검사하는거 하나더 추가 정보
일반 wrest.js에 숫자검사하는거 하나더 추가본문
숫자인지 검사하는것이 빠져있어서 하나더 추가했습니다.
필요하시면 복사해서 붙여넣기하세요
------------------------------------------------------------
if (REQUIRE_ONCE == null)//가장뒤에 이파일은 넣어준다
{
// 한번만 실행되게
var REQUIRE_ONCE = true;
/*
** 저작권자 : http://sir.co.kr
** e-mail : http://sir.co.kr)의 동의를 얻지 않고 배포, 수정이 자유롭습니다.
** 추가된 기능이 있다면 저에게도 보내주십시오. 다른분들에게 도움이 됩니다.
** 필요 파일 : wrest.js, wrest.css, wrest.gif
*/
var wrestMsg = "";
var wrestFld = null;
var wrestFldDefaultColor = "f5ffff";
var wrestFldBackColor = "ffe4e1";
var arrAttr = new Array ("required", "trim", "minlength", "email", "nospace","numeric");
// subject 속성값을 얻어 return, 없으면 tag의 name을 넘김
function wrestItemname(fld)
{
var itemname = fld.getAttribute("itemname");
if (itemname != null && itemname != "")
return itemname;
else
return fld.name;
}
// 양쪽 공백 없애기
function wrestTrim(fld)
{
var pattern = /(^\s*)|(\s*$)/g; // \s 공백 문자
fld.value = fld.value.replace(pattern, "");
return fld.value;
}
// 필수 입력 검사
function wrestRequired(fld)
{
if (wrestTrim(fld) == "") {
if (wrestFld == null) {
wrestMsg = wrestItemname(fld) + " : 필수 입력입니다.\n";
wrestFld = fld;
}
}
}
// 최소 길이 검사
function wrestMinlength(fld)
{
var len = fld.getAttribute("minlength");
if (fld.value.length < len) {
if (wrestFld == null) {
wrestMsg = wrestItemname(fld) + " : 최소 " + len + "자 이상 입력하세요.\n";
wrestFld = fld;
}
}
}
// 전자메일주소 형식 검사
function wrestEmail(fld)
{
if (!wrestTrim(fld)) return;
//var pattern = /(\S+)@(\S+)\.(\S+)/; 전자메일주소에 한글 사용시
var pattern = /([0-9a-zA-Z_-]+)@([0-9a-zA-Z_-]+)\.([0-9a-zA-Z_-]+)/;
if (!pattern.test(fld.value)) {
if (wrestFld == null) {
wrestMsg = wrestItemname(fld) + " : 전자메일주소 형식이 아닙니다.\n";
wrestFld = fld;
}
}
}
// 숫자인지검사
function wrestNumeric(fld)
{
// if (!wrestTrim(fld)) return;
if (fld.value.length > 0) {
for (i = 0; i < fld.value.length; i++) {
if (fld.value.charAt(i) < '0' || fld.value.charAt(i) > '9') {
wrestMsg = wrestItemname(fld) + " : 숫자가 아닙니다.\n";
wrestFld = fld;
}
}
}
}
// 공백 검사후 공백을 "" 로 변환
function wrestNospace(fld)
{
var pattern = /(\s)/g; // \s 공백 문자
if (pattern.test(fld.value)) {
if (wrestFld == null) {
wrestMsg = wrestItemname(fld) + " : 공백이 없어야 합니다.\n";
wrestFld = fld;
}
}
}
// submit 할 때 속성을 검사한다.
function wrestSubmit()
{
wrestMsg = "";
wrestFld = null;
var attr = null;
// 해당폼에 대한 요소의 갯수만큼 돌려라
for (var i = 0; i < this.elements.length; i++) {
// Input tag 의 type 이 text, file, password 일때만
if (this.elements[i].type == "text" ||
this.elements[i].type == "file" ||
this.elements[i].type == "password" ||
this.elements[i].type == "textarea") {
// 배열의 길이만큼 돌려라
for (var j = 0; j < arrAttr.length; j++) {
// 배열에 정의한 속성과 비교해서 속성이 있거나 값이 있다면
if (this.elements[i].getAttribute(arrAttr[j]) != null) {
// 기본 색상으로 돌려놓고
this.elements[i].style.backgroundColor = wrestFldDefaultColor;
switch (arrAttr[j]) {
case "required" : wrestRequired(this.elements[i]); break;
case "trim" : wrestRequired(this.elements[i]); break;
case "minlength" : wrestMinlength(this.elements[i]); break;
case "email" : wrestEmail(this.elements[i]); break;
case "nospace": wrestNospace(this.elements[i]); break;
case "numeric" : wrestNumeric(this.elements[i]);break;
default : break;
}
}
}
}
}
// 필드가 null 이 아니라면 오류메세지 출력후 포커스를 해당 오류 필드로 옮김
// 오류 필드는 배경색상을 바꾼다.
if (wrestFld != null) {
alert(wrestMsg);
wrestFld.style.backgroundColor = wrestFldBackColor;
wrestFld.focus();
return false;
}
if (this.oldsubmit && this.oldsubmit() == false) {
return false;
}
return true;
}
// 초기에 onsubmit을 가로채도록 한다.
function wrestInitialized()
{
for (var i = 0; i < document.forms.length; i++) {
// onsubmit 이벤트가 있다면 저장해 놓는다.
if (document.forms[i].onsubmit) document.forms[i].oldsubmit = document.forms[i].onsubmit;
document.forms[i].onsubmit = wrestSubmit;
for (var j = 0; j < document.forms[i].elements.length; j++) {
// 필수 입력일 경우는 * 배경이미지를 준다.
if (document.forms[i].elements[j].getAttribute("required") != null) {
document.forms[i].elements[j].style.backgroundColor = wrestFldDefaultColor;
/*
document.forms[i].elements[j].className = "wrest_required";
document.forms[i].elements[j].style.backgroundPosition = "top right";
document.forms[i].elements[j].style.backgroundRepeat = "no-repeat";
*/
}
}
}
}
wrestInitialized();
}
--------------------<이상끝!! ^ . ^ >------------------
필요하시면 복사해서 붙여넣기하세요
------------------------------------------------------------
if (REQUIRE_ONCE == null)//가장뒤에 이파일은 넣어준다
{
// 한번만 실행되게
var REQUIRE_ONCE = true;
/*
** 저작권자 : http://sir.co.kr
** e-mail : http://sir.co.kr)의 동의를 얻지 않고 배포, 수정이 자유롭습니다.
** 추가된 기능이 있다면 저에게도 보내주십시오. 다른분들에게 도움이 됩니다.
** 필요 파일 : wrest.js, wrest.css, wrest.gif
*/
var wrestMsg = "";
var wrestFld = null;
var wrestFldDefaultColor = "f5ffff";
var wrestFldBackColor = "ffe4e1";
var arrAttr = new Array ("required", "trim", "minlength", "email", "nospace","numeric");
// subject 속성값을 얻어 return, 없으면 tag의 name을 넘김
function wrestItemname(fld)
{
var itemname = fld.getAttribute("itemname");
if (itemname != null && itemname != "")
return itemname;
else
return fld.name;
}
// 양쪽 공백 없애기
function wrestTrim(fld)
{
var pattern = /(^\s*)|(\s*$)/g; // \s 공백 문자
fld.value = fld.value.replace(pattern, "");
return fld.value;
}
// 필수 입력 검사
function wrestRequired(fld)
{
if (wrestTrim(fld) == "") {
if (wrestFld == null) {
wrestMsg = wrestItemname(fld) + " : 필수 입력입니다.\n";
wrestFld = fld;
}
}
}
// 최소 길이 검사
function wrestMinlength(fld)
{
var len = fld.getAttribute("minlength");
if (fld.value.length < len) {
if (wrestFld == null) {
wrestMsg = wrestItemname(fld) + " : 최소 " + len + "자 이상 입력하세요.\n";
wrestFld = fld;
}
}
}
// 전자메일주소 형식 검사
function wrestEmail(fld)
{
if (!wrestTrim(fld)) return;
//var pattern = /(\S+)@(\S+)\.(\S+)/; 전자메일주소에 한글 사용시
var pattern = /([0-9a-zA-Z_-]+)@([0-9a-zA-Z_-]+)\.([0-9a-zA-Z_-]+)/;
if (!pattern.test(fld.value)) {
if (wrestFld == null) {
wrestMsg = wrestItemname(fld) + " : 전자메일주소 형식이 아닙니다.\n";
wrestFld = fld;
}
}
}
// 숫자인지검사
function wrestNumeric(fld)
{
// if (!wrestTrim(fld)) return;
if (fld.value.length > 0) {
for (i = 0; i < fld.value.length; i++) {
if (fld.value.charAt(i) < '0' || fld.value.charAt(i) > '9') {
wrestMsg = wrestItemname(fld) + " : 숫자가 아닙니다.\n";
wrestFld = fld;
}
}
}
}
// 공백 검사후 공백을 "" 로 변환
function wrestNospace(fld)
{
var pattern = /(\s)/g; // \s 공백 문자
if (pattern.test(fld.value)) {
if (wrestFld == null) {
wrestMsg = wrestItemname(fld) + " : 공백이 없어야 합니다.\n";
wrestFld = fld;
}
}
}
// submit 할 때 속성을 검사한다.
function wrestSubmit()
{
wrestMsg = "";
wrestFld = null;
var attr = null;
// 해당폼에 대한 요소의 갯수만큼 돌려라
for (var i = 0; i < this.elements.length; i++) {
// Input tag 의 type 이 text, file, password 일때만
if (this.elements[i].type == "text" ||
this.elements[i].type == "file" ||
this.elements[i].type == "password" ||
this.elements[i].type == "textarea") {
// 배열의 길이만큼 돌려라
for (var j = 0; j < arrAttr.length; j++) {
// 배열에 정의한 속성과 비교해서 속성이 있거나 값이 있다면
if (this.elements[i].getAttribute(arrAttr[j]) != null) {
// 기본 색상으로 돌려놓고
this.elements[i].style.backgroundColor = wrestFldDefaultColor;
switch (arrAttr[j]) {
case "required" : wrestRequired(this.elements[i]); break;
case "trim" : wrestRequired(this.elements[i]); break;
case "minlength" : wrestMinlength(this.elements[i]); break;
case "email" : wrestEmail(this.elements[i]); break;
case "nospace": wrestNospace(this.elements[i]); break;
case "numeric" : wrestNumeric(this.elements[i]);break;
default : break;
}
}
}
}
}
// 필드가 null 이 아니라면 오류메세지 출력후 포커스를 해당 오류 필드로 옮김
// 오류 필드는 배경색상을 바꾼다.
if (wrestFld != null) {
alert(wrestMsg);
wrestFld.style.backgroundColor = wrestFldBackColor;
wrestFld.focus();
return false;
}
if (this.oldsubmit && this.oldsubmit() == false) {
return false;
}
return true;
}
// 초기에 onsubmit을 가로채도록 한다.
function wrestInitialized()
{
for (var i = 0; i < document.forms.length; i++) {
// onsubmit 이벤트가 있다면 저장해 놓는다.
if (document.forms[i].onsubmit) document.forms[i].oldsubmit = document.forms[i].onsubmit;
document.forms[i].onsubmit = wrestSubmit;
for (var j = 0; j < document.forms[i].elements.length; j++) {
// 필수 입력일 경우는 * 배경이미지를 준다.
if (document.forms[i].elements[j].getAttribute("required") != null) {
document.forms[i].elements[j].style.backgroundColor = wrestFldDefaultColor;
/*
document.forms[i].elements[j].className = "wrest_required";
document.forms[i].elements[j].style.backgroundPosition = "top right";
document.forms[i].elements[j].style.backgroundRepeat = "no-repeat";
*/
}
}
}
}
wrestInitialized();
}
--------------------<이상끝!! ^ . ^ >------------------
추천
6
6
댓글 전체
영문파일명이 아니면 안됩니다 메시지뜨도록.`~^^;;
영문파일명 검사하는거도 넣어주세요..~~~^^;;